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

Chapter-1 Exception Handling in Phython

The document discusses exception handling in Python including types of errors, built-in exceptions, raising exceptions, and handling exceptions through try, except, and finally blocks. Exception handling prevents programs from crashing by capturing errors and providing defined responses.

Uploaded by

t29.sammat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Chapter-1 Exception Handling in Phython

The document discusses exception handling in Python including types of errors, built-in exceptions, raising exceptions, and handling exceptions through try, except, and finally blocks. Exception handling prevents programs from crashing by capturing errors and providing defined responses.

Uploaded by

t29.sammat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER-1

Exception Handling in Python


Types of Errors: Syntax errors, runtime errors or logical errors.
SYNTAX ERRORS:
 Syntax errors are detected when we have not followed
the rules of the particular programming language
while writing a program.
 These errors are also known as parsing errors.
EXCEPTIONS:
 An exception is a Python object that represents an error.
 Runtime errors are also known as Exceptions.
BUILT-IN EXCEPTIONS
 The appropriate exception handler code is executed which displays the reason along with the
raised exception name. The programmer then has to take appropriate action to handle it.
 A programmer can also create custom exceptions to suit one’s requirements. These are called user-
defined exceptions.
 Table 1.1 Built-in exceptions in Python
RAISING EXCEPTIONS -the Python interpreter raises (throws) an exception.
Programmers can also forcefully raise exceptions in a program using the raise and assert
statements.
The raise Statement
Python also displays a stack Traceback. This is a structured block of text that contains
information about the sequence of function calls that have been made in the branch of
execution of code in which the exception was raised.
The assert Statement used to test an expression in the program code. If the result after
testing comes false, then the exception is raised.
The syntax for assert statement is: assert Expression[,arguments]
HANDLING EXCEPTIONS:
 Each and every exception ha to be handled by the programmer to avoid the program
from crashing abruptly.
 This is done by writing additional code in a program to give proper messages or
instructions to the user on encountering an exception.
 This process is known as Exception handling.
Need for Exception Handling:
Exception handling is being used not only in Python programming but in most programming
languages like C++, Java, Ruby, etc. It is a useful technique that helps in capturing runtime
errors and handling them so as to avoid the program getting crashed.
the important points regarding exceptions and their handling:
• Python categorises exceptions into distinct types so that specific exception handlers (code
to handle that particular exception) can be created for each type.
• Exception handlers separate the main logic of the program from the error detection and
correction code. The segment of code where there is any possibility of error or exception, is
placed inside one block. The code to be executed in case the exception has occurred, is
placed inside another block. These statements for detection and reporting the exception do
not affect the main logic of the program.
• The compiler or interpreter keeps track of the exact position where the error has occurred.
• Exception handling can be done for both user-defined and built-in exceptions.
Process of Handling Exception
When an error occurs, Python interpreter
creates an object called the exception object.
This object contains information about the
error like its type, file name and position in the
program where the error has occurred. The
object is handed over to the runtime system so
that it can find an appropriate code to handle
this particular exception.
This process of creating an exception object
and handing it over to the runtime system is
called throwing an exception.
The runtime system searches the entire
program for a block of code, called the exception handler that can handle the raised
exception. It first searches for the method in which the error has occurred and the exception
has been raised.
This hierarchical search in reverse order continues till the exception handler is found. This
entire list of methods is known as call stack. When a suitable handler is found in the call
stack, it is executed by the runtime process. This process of executing a suitable handler is
known as catching the exception. If the runtime system is not able to find an appropriate
exception after searching all the methods in the call stack, then the program execution stops.

Catching Exceptions An exception is said to be caught when a code that is designed to


handle a particular exception is executed. Exceptions, if any, are caught in the try block and
handled in the except block.

Sometimes, a single piece of code might be suspected to have more than one type of error.
For handling such situations, we can have multiple except blocks for a single try block.

In the code, two types of exceptions (ZeroDivisionError and ValueError) are handled using
two except blocks for a single try block. When an exception is raised, a search for the
matching except block is made till it is handled. If no match is found, then the program
terminates. However, if an exception is raised for which no handler is created by the
programmer, then such an exception can be handled by adding an except clause without
specifying any exception. This except clause should be added as the last clause of the
try..except block.

try...except…else clause We can put an optional else clause along with the try...except
clause.

But if there is no error then none of the except blocks will be executed. In this case, the
statements inside the else clause will be executed.

FINALLY CLAUSE The try statement in Python can also have an optional finally clause.
The statements inside the finally block are always executed regardless of whether an
exception has occurred in the try block or not. It is a common practice to use finally clause
while working with files to ensure that the file object is closed.

SUMMARY
• Syntax errors or parsing errors are detected when we have not followed the rules of the
particular programming language while writing a program.

• When syntax error is encountered, Python displays the name of the error and a small
description about the error.

• The execution of the program will start only after the syntax error is rectified. • An
exception is a Python object that represents an error.

• Syntax errors are also handled as exceptions.

• The exception needs to be handled by the programmer so that the program does not
terminate abruptly.

• When an exception occurs during execution of a program and there is a built-in exception
defined for that, the error message written in that exception is displayed. The programmer
then has to take appropriate action and handle it.

• Some of the commonly occurring built-in exceptions are SyntaxError, ValueError,


IOError, KeyboardInterrupt, ImportError, EOFError, ZeroDivisionError, IndexError,
NameError, IndentationError, TypeError,and OverFlowerror.

• When an error is encountered in a program, Python interpreter raises or throws an


exception.

• Exception Handlers are the codes that are designed to execute when a specific exception is
raised.

• Raising an exception involves interrupting the normal flow of the program execution and
jumping to the exception handler.

• Raise and assert statements are used to raise exceptions.


• The process of exception handling involves writing additional code to give proper
messages or instructions to the user. This prevents the program from crashing abruptly. The
additional code is known as an exception handler.

• An exception is said to be caught when a code that is designed to handle a particular


exception is executed.

• An exception is caught in the try block and handles in except block.

• The statements inside the finally block are always executed regardless of whether an
exception occurred in the try block or not.

You might also like