0% found this document useful (0 votes)
6 views

Exception Handling

Uploaded by

XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exception Handling

Uploaded by

XYZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Exception Handling

Types of Errors
• We can broadly classify errors into two categories - Syntax Error and Exception
Errors.

• Syntax Errors → This occurs when there is a wrong syntax in our code.

• Exception Errors →
• When there is no syntax error in our code and our code gets executed but some error
occurs during the execution, then this type of error is called Exception Error.
• When PVM cannot execute the byte code, it flags runtime error.
• For example,
• dividing a number with 0 is also an exception error, etc.
• Insufficient memory to store something
A Python Program to Understand the
effect of an Exception

• PVM is displaying error “divide by zero” and terminating


program at line no 5
• Due to this abnormal termination, the subsequent
statements in program are not executed.
• Hence, f.close() is not executed and the file which is
opened in the beginning of the program is not closed.
• This leads to loss of entire data that is already present in
the file.
Effect of sudden program
termination
• When there is error in a program, due to its sudden termination, the
following things can be suspected:
• The important data in the files or databases used in the program may be lost
• The software may be corrupted
• The program abruptly terminated giving error message to the user making
the user confused.
• It is duty of programmers to handle the errors.
• We cannot handle all types of errors (for example hardware failure),
but some types of errors which can be handled are called Exceptions.
Exception
• It is a runtime error which can be handled by the programmer.
• That means if the programmer can guess an error in the program and he can do
something to eliminate the harm caused by that error, then it is called an
‘exception’.
• If the programmer cannot do anything in case of an error, then it is called an
‘error’ and not an exception.
• All exceptions are represented as classes in Python.
• The exceptions that are already available in Python are called ‘built-in exceptions’.
• The base class for all ‘built-in exceptions’ is ‘BaseException’ class.
Exception
• The purpose of exception handling is to make the program robust.
• The word ‘robust’ means ‘strong’.
• A robust program does not terminate in the middle.
• Also, when there is an error in the program, it will display an appropriate
message to the user and continue execution.
• Designing such programs is needed in any software development.
• For this purpose, the programmer should handle the errors.
• When the errors can be handled, they are called exception.
Steps for Exception Handling
1. The programmer should observe the statements in his program where there may be a possibility of
exceptions. Such statements should be written inside a ‘try’ block.
try:
statements
2. The programmer should write the ‘except’ block where he should display the exception details to the
user. This helps the user to understand that there is some error in the program. The programmer
should also display a message regarding what can be done to avoid the error.
except exceptionname:
statements
3. Lastly, the programmer should perform the clean up actions like closing the files and terminating any
other processes which are running. The programmer should write this code in the finally block.
finally:
statements
The speciality of finally block is that it will be executed irrespective of whether there is an exception or not.
Program to handle the
ZeroDivisionError Exception
General syntax for exception
handling
try:
statements
Except Exception1:
handler1
Except Exception 2:
handler 2
else: // If no exception is raised the statements inside the ‘else’ block are executed
statements
finally:
statements
Points to Note
1. A single try block can be followed by several except blocks
2. Multiple except blocks can be used to handle multiple exceptions
3. We cannot write except blocks without a try block
4. We can write a try block without any except blocks
5. Else block and finally block are not compulsory
6. When there is no exception, else block is executed after try block
7. Finally block is always executed

You might also like