The document discusses error handling in Python programming, highlighting the types of errors (syntax errors and exceptions) and the importance of managing them to prevent program failures. It introduces keywords such as try, except, and finally for handling errors, along with examples demonstrating their usage. Additionally, it covers specifying exceptions and creating custom exceptions for better debugging and control over error management.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
10-python-error-handling
The document discusses error handling in Python programming, highlighting the types of errors (syntax errors and exceptions) and the importance of managing them to prevent program failures. It introduces keywords such as try, except, and finally for handling errors, along with examples demonstrating their usage. Additionally, it covers specifying exceptions and creating custom exceptions for better debugging and control over error management.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35
Error Handling
Engr. Hiroyoshi DG. Arai
Error. Error? Error! • In programming, there are cases where errors can occur not because your code is bad, but because the inputs don’t match the data type of the function. • For example, converting a letter into integer will cause a ValueError. • Or simply, the mathematical operation is just not possible, such as dividing by zero which will cause ZeroDivisionError Error. Error? Error! • Getting these errors can lead to unexpected behavior or even stop the program from executing. • This is why programs should always handle these errors in such way when an error occurs, the program will be able to handle it and will still continue to function. Error. Error? Error! • In python programming, there are 2 kinds of errors: – Syntax Error These are the errors from missing a symbol, or not following the format of a function. – Exceptions these are the errors that the python will raise when an exact error occurred. Error Handling Keywords • Python provides a way to handle these errors in an easy and simple format by using these keywords: – try – except – finally try Keyword • This keyword means to try to execute the block of code until it produces an error. • If it doesn’t produce an error, it will just continue and do nothing special. • However, if it encounters an error, it will raise an exception. except Keyword • This keyword executes the block of code when an error occurred. • This basically catches the program from failing and gives an instruction on what to do in case of an error. finally Keyword • This keyword executes regardless of what happens to the code inside the try-except block. • This part can be omitted, but it can be useful for some cases such as file handling. Error Example >>> print(“Today, we will divide a number by zero”) >>> print(30 / 0) Today, we will divide a number by zero Traceback (most recent call last): File “<pyshell#0>”, line 2 in <module> print(30/0) ZeroDivisionError: division by zero >>>
• ZeroDivisionError was raised because you cannot divide
by zero arithmetically. try – except Example >>> try: >>> print(“Today, we will divide a number by zero”) >>> print(30 / 0) >>> except: >>> print(“Hey! You cannot divide by zero!”) Today, we will divide a number by zero Hey! You cannot divide by zero! >>>
• Here, error occurred at line 3, however, since it is under a try
block, it raised an exception. except block of code will execute. try – except-finally Example >>> try: >>> print(“Today, we will divide a number by zero”) >>> print(30 / 0) >>> except: >>> print(“Hey! You cannot divide by zero!”) >>> finally: >>> print(“I’ll do this regardless”) Today, we will divide a number by zero Hey! You cannot divide by zero! I’ll do this regardless try – except-finally Example >>> try: >>> print(“Today, we will divide a number by one”) >>> print(30 / 1) >>> except: >>> print(“Hey! You cannot divide by zero!”) >>> finally: >>> print(“I’ll do this regardless”) Today, we will divide a number by zero 30 I’ll do this regardless Specifying exceptions • except keyword is a very strong tool in python programming, however, most new programmers often use it in such a general way to except ALL errors. • By doing this, you will have a silent error and can cause trouble when debugging. • The best practice is to always specify what exception to except in order to have a better flow for your program. Specifying exception >>> try: >>> print(“Today, we will divide a number by one”) >>> print(30 / 0) >>> except ZeroDivisionError: >>> print(“Hey! You cannot divide by zero!”) >>> finally: >>> print(“I’ll do this regardless”) Today, we will divide a number by zero 30 I’ll do this regardless Specifying multiple exception >>> try: >>> names = [“Alan”, “Bob”, “Cassie”, “Daisy”] >>> print(names[5]) >>> except ZeroDivisionError: >>> print(“Hey! You cannot divide by zero!”) >>> except IndexError: >>> print(“Index is out of bound.”) Index is out of bound. Exceptions • Python has over 60 built-in exceptions that it can raise when an error is encountered. • These exceptions are organized into two groups: – Base class exceptions – Concrete exceptions Base Class Exceptions • You can think of these as the general exceptions for most errors. – BaseException base class for all built-in exceptions. This also includes SystemExit, KeyboardInterrupt, GeneratorExit exceptions. – Exception base class for all built-in non-system-exiting exceptions. – ArithmeticError base class for all built-in arithmetic errors. – BufferError raised when a buffer related operation cannot be performed. – LookupError raised when a key or index used on a sequence is invalid. Concrete Exceptions • These are the more specific exceptions that python will raise under the base class exception. Examples are: – ImportError raised when the import statement fails to load a module. – IndexError raised when the sequence subscript is out of range. – NameError raised when a local or global name is not found. – TypeError raised when an operation or function is applied to an object of inappropriate type. – ValueError raised when an operation or function receives an argument that has the right type but an inappropriate value. Exception Table Exception Table Exception Table Exception Table Exception Table Exception Heirarchy Custom Exception Here is the OOP part!
Engr. Hiroyoshi DG. Arai
Custom Exceptions • These exceptions are classes. • From one of the pillar of OOP inheritance, it is possible to inherit a class. • Therefore, we can define a class that inherits these exception to create a custom exception. • These custom exceptions are useful for debugging programs when you already know what exactly went wrong with it. Custom Exceptions raise keyword • this keyword is used to raise an exception and stops the control flow of the program. Custom Exceptions Examples Custom Exceptions Examples Custom Exceptions Examples Custom Exceptions Examples Custom Exceptions Examples