0% found this document useful (0 votes)
19 views15 pages

12.exception

The document explains two types of errors in Python: Syntax errors, which occur due to incorrect syntax and stop program execution, and Exceptions, which disrupt the normal flow of a program but do not stop it. It discusses how to handle exceptions using try and except statements, allowing for specific exception handling. Additionally, it lists common exceptions such as IndexError, ValueError, and ZeroDivisionError, along with their causes.

Uploaded by

JEAN ROGER
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)
19 views15 pages

12.exception

The document explains two types of errors in Python: Syntax errors, which occur due to incorrect syntax and stop program execution, and Exceptions, which disrupt the normal flow of a program but do not stop it. It discusses how to handle exceptions using try and except statements, allowing for specific exception handling. Additionally, it lists common exceptions such as IndexError, ValueError, and ZeroDivisionError, along with their causes.

Uploaded by

JEAN ROGER
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/ 15

Exception in python

Syntax errors and Exceptions


 Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors
are the problems in a program due to which the program will stop the
execution.
 Syntax Error: As the name suggests this error is caused by the wrong
syntax in the code. It leads to the termination of the program.
 Exceptions are raised when some internal events occur which changes the
normal flow of the program.
Exception in python
 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.
 Even if a statement or expression is syntactically correct, it may
cause an error when an attempt is made to execute it. Errors
detected during execution are called exceptions.

 FileNotFoundError, ZeroDivisionError ,ImportError


Syntax errors and Exceptions
 Exceptions: Exceptions are raised when the program is syntactically
correct, but the code resulted in an error. This error does not stop the
execution of the program, however, it changes the normal flow of the
program.
a=[1,2,3]
print("second element ",a[1])
print("third element ",a[3])

E:\python>python e.py
second element 2
Traceback (most recent call last):
File "E:\python\e.py", line 4, in <module>
print("third element ",a[3])
~^^^
IndexError: list index out of range
Try and Except Statement – Catching Exceptions
 Try and except statements are used to catch and handle exceptions in
Python. Statements that can raise exceptions are kept inside the try clause
and the statements that handle the exception are written inside except
clause.

a=[1,2,3]
try:
E:\python>python e.py
print("third element An error occured
",a[3]) outside try-except
except:
print("An error occured")

print("outside try-except")
Handling Exceptions
Catching Specific Exception

 A try statement can have more than one except clause, to specify handlers for
different exceptions. At most one handler will be executed. For example, we can add
IndexError in the above code. The general syntax for adding specific exceptions are –
a=[1,2,3]
try:
print("third element ",a[3])

except IndexError:
print("An index error occured check ur code")
except:
print("An error occured")

print("outside try-except")

E:\python>python e.py
An index error occured check ur code
outside try-except
Try-except block
 try: the code with the exception(s) to catch. If an exception is
raised, it jumps straight into the except block.
 except: this code is only executed if an exception occured in
the try block. The except block is required with a try block,
even if it contains only the pass statement.
 else: Code in the else block is only executed if no exceptions
were raised in the try block.
 finally: The code in the finally block is always executed,
regardless of if an exception was raised or not.
Try-except-else-finally

Type error
 TypeError: This exception is raised when an operation or function is
applied to an object of the wrong type, such as adding a string to an
integer.
ValueError
 Python ValueError is raised when a function receives an argument of the correct
type but an inappropriate value.
ValueError
 Python ValueError is raised when a function receives an argument of the correct
type but an inappropriate value.
 NameError: This exception is raised when a variable or function name is not
found in the current scope.
 IndexError: This exception is raised when an index is out of range for a list,
tuple, or other sequence types.
 KeyError: This exception is raised when a key is not found in a dictionary.
 AttributeError: This exception is raised when an attribute or method is not
found on an object, such as trying to access a non-existent attribute of a class
instance.
 IOError: This exception is raised when an I/O operation, such as reading or
writing a file, fails due to an input/output error.
 ZeroDivisionError: This exception is raised when an attempt is made to divide
a number by zero.
 ImportError: This exception is raised when an import statement fails to find or
load a module.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy