In this post, we will study Python Exception handling, how we can deal with exceptions in Python. we will see with examples.
Exceptions are an integral part of any programming language. As we write code in any language, there are so many chances to get exceptions or errors in that code. So every programming language takes care of the exceptions that occur in the programs, So Python also does the same so that Python code can flow uninterrupted.
Exceptions are the unwanted process or inputs which interrupt the flow of the program and halts the execution.
So, we need some mechanism or way to handle these unwanted exceptions.
Let’s see an example with Exception handling
[code language=”python”]
a=int(input("please enter any integer number :"))
print("You enter :",a)
[/code]
Output
please enter any integer number : 22 You enter : 22
In above example, we asked the user to put integer number and print the same integer number. what if user does not put the integer number then what will happens, let’s see
Output
please enter any integer number :Python ValueError: invalid literal for int() with base 10: 'Python'
If we run same program and this time lets put value Python instead of some integer, we will get Exception ValueError, says that invalid value, because program was expecting integer value but we pass String value results it stops the next line of program to execute.
So, In Python we need Exception handling ways to handle these type of exceptions, so that we can run our program effectively. Now we will see how we will handle exceptions in Python.
Use of try except block
In Python we can handle exceptions using try except block. Suspected lines of code can be put inside try block and if any exception occur in the suspected lines of code then except block will take care of that.
Syntax of try except block
try : ///suspected lines of code .... except: // handle the exception ...
In try block we put the code inside try block whose chances of exception occur is much higher and if exception occur then except block take care of it.
Let’s see same example with exception handling way.
[code language=”python”]
try:
a=int(input("please enter any integer number :"))
print("You enter :",a)
except:
print("There is something wrong. you might be enter wrong value")
[/code]
Output
please enter any integer number :Python There is something wrong. you might be enter wrong value
Now if you put Python as input value, you will see that exception did not come, instead of exception, we get user friendly message from except block. it means in above example we handle the exception.
Types of Errors
Error is also kind of exception. There are two types of errors
- Syntax Error
- Exceptions
Syntax Error occur when there is any programming syntax related issue come. See below example
[code language=”python”]
a=input "please enter any integer number :")
print("You enter :",a)
[/code]
Output
a=input "please enter any integer number :") ^ SyntaxError: invalid syntax
In above example, there is syntax error, we forgot to put round bracket at the starting.
So these kind of error are syntax errors.
Exceptions
Little bit we read about exceptions in staring.
Built-in exceptions
User defined exceptions