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

Exception Handling in Python

record

Uploaded by

psaritha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exception Handling in Python

record

Uploaded by

psaritha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exception

Handling
Exception Handling
• Exception handling is done by try and catch block.
• Suspicious code that may raise an exception, this kind of
code will be placed in try block.
• A block of code which handles the problem is placed in
except block.
try ... except

• In Python, exceptions can be handled using a try


statement.
• A critical operation which can raise exception is placed
inside the try clause and the code that handles exception
is written in except clause.
• It is up to us, what operations we perform once we have
caught the exception. Here is a simple example.
try…except…inbuilt exception
try ... except ... else clause
• Else part will be executed only if the try block doesn’t
raise an exception.
• Python will try to process all the statements inside try
block. If value error occurs, the flow of control will
immediately pass to the except block and remaining
statement in try block will be skipped.
try ... except…finally
• A finally clause is always executed before leaving the try
statement, whether an exception has occurred or not.
try…multiple exception
Raising Exceptions
• In Python programming, exceptions are raised when
corresponding errors occur at run time, but we can
forcefully raise it using the keyword raise.

• Syntax:
>>> raise error name

You might also like