Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Exception.py
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#python 3.5.2 #Exception Handling #Errors: #1)Compile time Errors:Errors which arise during compilation of the application due to syntactical mistakes are compile time errors. #Example : If You mis spell keywords you will get Compile tile error, etc. #These errors can be resolved at the time of development of the application. #2)Logical Errors:These errors arise because of incorrect logic. Because of incorrect logic we get incorrect output. #Example : 2+3=5(expected) but suppose your program is giving you 6 as output then it's a logical error.you can to the respective code segment and rectify it. #These errors can be resolved at testing phase #)Run time Errors:These erors arise at the run time of the application because of some input sets. #Example : a/b if a=10 and b=5 output willbe 2 but if a=10 and b=0 then you will get an error for this input set and your program terminates abruptly as soon as it gets an error like this #and remaining task will not be executed and also it will display error to the user which is technical which the user might not understand.So as a programmer we need to handle these type of errors #very carefully and tell the user about error in layman's language #These errors can be handled by using try: , except: , and finally: block #Among try and except only one gets executed at time but finally always executes #a=10#normal stmt #b=5 #print(a/b)critical stmt #print('End reached') #a=10 #b=0 #print(a/b)#Error arises and program terminates here abnormally #print('End reached')#this will not get executed #a=10 #b=2 #try: #print(a/b)#Critical stmt #except Exception: #print('Hey, you can\'t divide a number by zero') #print('End reached') #a=10 #b=0 #try: #print(a/b)#Critical stmt #except Exception: #print('Hey, you can\'t divide a number by zero') #print('End reached') #It's always a good practice to close a resource at the end when you are done with the resource #a=10 #b=0 #try: #print('Resource open') #print(a/b)#Critical stmt #print('Resource closed') #except Exception as e: #print('Hey, you can\'t divide a number by zero') #print('End reached') #a=10 #b=0 #try: #print('Resource open') #print(a/b)#Critical stmt #except Exception as e: #print('Hey, you can\'t divide a number by zero') #print('Resource closed') #print('End reached') #a=10 #b=2 #try: #print('Resource open') #print(a/b)#Critical stmt #except Exception as e: #print('Hey, you can\'t divide a number by zero') #print('Resource closed') #print('End reached') #a=10 #b=2 #try: #print('Resource open') #print(a/b)#Critical stmt #except Exception as e: #print('Hey, you can\'t divide a number by zero') #finally: #print('Resource closed') #print('End reached') a=10 b=0 try: print('Resource open') print(a/b)#Critical stmt except Exception as e: print('Hey, you can\'t divide a number by zero') finally: print('Resource closed') print('End reached')
[
+
]
Show input
Absolute running time: 0.71 sec, cpu time: 0.21 sec, memory peak: 6 Mb, absolute service time: 0,76 sec
edit mode
|
history
|
discussion
Resource open Hey, you can't divide a number by zero Resource closed End reached