Exception handling in python by R4R Team

What is exception-
-An exception is an event which occurs during the execution of a program that break the normal flow of the program's instructions.

Exception handling-
- We can handle exception in python by try and except block.
- Try block is a block in which we write that part of program where chances of exception will occur.
- Except is a part where we handle the generated exceptions or this block is run after the exception generate.

Keywords that are used in exception handling is-
- try
- except
- finially
- raise

Most common error-

- ZeroDivisionError: Occurs when any thing is divided by zero.
- NameError: It occurs when a variable name is not found. It may be local or global.
- IndentationError: if incorrect indentation, python support indentation.
- IOError: When input output task is fails then this error occur.
- EOFError: It occurs when the end of the file is reached, and yet operations are being performed.
- IndexError : when task is perform at out of index range.

-ZeroDivisionError

program-

try:
n=1/0
except ZeroDivisionError:
print("Number is divide by 0")


output-

Number is divide by 0


IndexError-

program-

l=[1,2,3,4]
try:
print(l[10])
except IndexError:
print("List is out of range")


output-

List is out of range




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!