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

4 - 1 Exceptions

Exceptions occur when something goes wrong in a program due to incorrect code or input. Common exceptions include ImportError, IndexError, NameError, SyntaxError, TypeError, and ValueError. To handle exceptions, a try/except statement can be used where the try block contains code that may cause an exception and the except block contains code to handle the exception. For example, a ZeroDivisionError exception can be handled by using an except ZeroDivisionError block to print an error message when code tries to divide a number by zero.

Uploaded by

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

4 - 1 Exceptions

Exceptions occur when something goes wrong in a program due to incorrect code or input. Common exceptions include ImportError, IndexError, NameError, SyntaxError, TypeError, and ValueError. To handle exceptions, a try/except statement can be used where the try block contains code that may cause an exception and the except block contains code to handle the exception. For example, a ZeroDivisionError exception can be handled by using an except ZeroDivisionError block to print an error message when code tries to divide a number by zero.

Uploaded by

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

Exceptions

Sololearn 4.1
Exceptions
You have already seen exceptions in previous code. They occur when something goes
wrong, due to incorrect code or input. When an exception occurs, the program
immediately stops.
The following code produces the ZeroDivisionError exception by trying to divide 7 by
0.
num1 = 7
num2 = 0
print(num1/num2)
Result:
>>>
ZeroDivisionError: division by zero
>>>
Exceptions
Different exceptions are raised for different reasons.
Common exceptions:
ImportError: an import fails;
IndexError: a list is indexed with an out-of-range number;
NameError: an unknown variable is used;
SyntaxError: the code can't be parsed properly;
TypeError: a function is called on a value of an inappropriate type;
ValueError: a function is called on a value of the correct type, but with an inappropriate
value.
Python has several other built-in exceptions, such as ZeroDivisionError andOSError. Third-
party libraries also often define their own exceptions.
Exception Handling
To handle exceptions, and to call code when an exception occurs, you can use a try/except statement.
The try block contains code that might throw an exception. If that exception occurs, the code in the try block stops being
executed, and the code in the except block is run. If no error occurs, the code in the except block doesn't run.
For example:
try:
num1 = 7
num2 = 0
print (num1 / num2)
print("Done calculation")
except ZeroDivisionError:
print("An error occurred")
print("due to zero division")

Result: >>> An error occurred due to zero division


In the code above, the except statement defines the type of exception to handle (in our case, the ZeroDivisionError).

You might also like