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

Exceptions in Python

An exception is an event that disrupts normal program flow, such as a runtime error. In Python, exceptions are objects that represent errors. When an exception occurs, the program must either handle it immediately or terminate. To handle exceptions, code can be placed inside a try block, with except blocks to handle specific exceptions. Common built-in exceptions include ZeroDivisionError, NameError, and IOError. Exceptions can provide additional error details through an argument variable in the except clause. Try-except blocks allow programs to gracefully handle errors.

Uploaded by

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

Exceptions in Python

An exception is an event that disrupts normal program flow, such as a runtime error. In Python, exceptions are objects that represent errors. When an exception occurs, the program must either handle it immediately or terminate. To handle exceptions, code can be placed inside a try block, with except blocks to handle specific exceptions. Common built-in exceptions include ZeroDivisionError, NameError, and IOError. Exceptions can provide additional error details through an argument variable in the except clause. Try-except blocks allow programs to gracefully handle errors.

Uploaded by

AKASH S R
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXCEPTIONS IN

PYTHON
Done By Akash Sr
WHAT IS EXCEPTION
An exception is an event, which occurs during the execution of a program that disrupts the
normal flow of the program's instructions. In general, when a Python script encounters a situation
that it cannot cope with, it raises an exception. An exception is a Python object that represents an
error.
When a Python script raises an exception, it must either handle the exception immediately
otherwise it terminates and quits.
Handling an exception
If you have some suspicious code that may raise an exception, you can defend your program by
placing the suspicious code in a try: block. After the try: block, include an except: statement,
followed by a block of code which handles the problem as elegantly as possible.
3

SYNTAX
Here is simple syntaxof try....except...else blocks −

try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
TYPES OF EXCEPTION
Exceptions can either belong to the in-built errors/exceptions or have custom exceptions.Some of
the common in-built exceptions are as follows:

• ZeroDivisionError
• NameError
• IndentationError
• IOError
• EOFError
5
• Handling an exception
• If you have some suspicious code that may raise an exception, you can defend your program by
placing the suspicious code in a try: block. After the try: block, include an except: statement, followed
by a block of code which handles the problem as elegantly as possible.

• Syntax
• Here is simple syntax of try....except...else blocks −

• try:
• You do your operations here;
• ......................
• except ExceptionI:
• If there is ExceptionI, then execute this block.
• except ExceptionII:
• If there is ExceptionII, then execute this block.
• ......................
• else:
• If there is no exception then execute this block.
• Here are few important points about the above-mentioned syntax −
6
• Argument of an Exception
• An exception can have an argument, which is a value that gives additional information about the problem. The
contents of the argument vary by exception. You capture an exception's argument by supplying a variable in
the except clause as follows −

• try:
• You do your operations here;
• ......................
• except ExceptionType, Argument:
• You can print value of Argument here...
• If you write the code to handle a single exception, you can have a variable follow the name of the exception in
the except statement. If you are trapping multiple exceptions, you can have a variable follow the tuple of the
exception.

• This variable receives the value of the exception mostly containing the cause of the exception. The variable
can receive a single value or multiple values in the form of a tuple. This tuple usually contains the error string,
the error number, and an error location.
THANK YOU

You might also like