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

Week 07 - Exception Handling

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

Week 07 - Exception Handling

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

Errors & Exceptions

Error Trapping

By Nishan Saliya IIT 1


Learning Outcome of This Session

1. What is an exception?
2. Error types during coding
3. Python Built-in Exceptions
4. Create custom exceptions
5. How to handle errors?

By Nishan Saliya IIT 2


What is an Exception?

 Exception is an event which will occur during the


execution of a program that will halt or disrupt
the flow of the program execution.
 A programmer should handle these errors properly
which will safeguard program execution, or it will
allow the program to recover from the exception

By Nishan Saliya IIT 3


Error Types in Coding

 Syntax Errors
 Runtime Errors
 Logical Errors

By Nishan Saliya IIT 4


Syntax Errors

 Easy to find
 Occurs during the coding, testing and
development
 Easily fixed
 Program is released once the syntax
errors are fixed
By Nishan Saliya IIT 5
Syntax Error – Example 1

By Nishan Saliya IIT 6


Syntax Error – Example 2

What are the details you see in this error message?

By Nishan Saliya IIT 7


Runtime Errors

 Moderately difficult to find


 Occurs during the deployment
 Once found, easy to fix

By Nishan Saliya IIT 8


Runtime

By Nishan Saliya IIT 9


Runtime Error – Example

By Nishan Saliya IIT 10


Logical Errors

 Hard to find
 Algorithm errors
 May visible during deployment not during coding,
testing and development
 Can be fixed if figured properly
 Program may be released with the logical error
 Shows no errors!
 Unpredicted behavior or output

By Nishan Saliya IIT 11


Logical Error – Example

By Nishan Saliya IIT 12


Exception Handling in Python

 Python uses 4 different keywords to handle errors


 We discuss 3 of them
 They are,
 try
 except
 else

By Nishan Saliya IIT 13


try … except

try

You ask the code to “try”


a segment. If it works, then no issues

By Nishan Saliya IIT 14


try … except

try
The “try” throws an error
if it fails to execute a
code properly
The “except” captures
the error. Programmer
should handle it.

except
By Nishan Saliya IIT 15
Example Exception – Case 1

By Nishan Saliya IIT 16


Example Exception Handling – Case 1

try

except

By Nishan Saliya IIT 17


Capture a Specific Error / Multiple
Errors

Specific Error

General Error

By Nishan Saliya IIT 18


Python Exceptions (Built-In)

 Some common exceptions are,


 NameError
 ImportError
 IndexError
 SyntaxError
 TypeError
 You can read more about these exceptions via
https://docs.python.org/3/library/exceptions.html#excep
tion-hierarchy
By Nishan Saliya IIT 19
By Nishan Saliya IIT 20
Your Own Exceptions

 You can create your own errors


 Raise your exception

By Nishan Saliya IIT 21


Raise Exception – Case 1

By Nishan Saliya IIT 22


Raise Exception Handling – Case 1

By Nishan Saliya IIT 23


Let’s Try 2 Simple Programs

By Nishan Saliya IIT 24


A Simple Program to See an Error

By Nishan Saliya IIT 25


Program Output

By Nishan Saliya IIT 26


A Simple Program Which Handles The
Error

By Nishan Saliya IIT 27


Program Output

By Nishan Saliya IIT 28


A Simple Program to Raise an Error

By Nishan Saliya IIT 29


Program Output

By Nishan Saliya IIT 30


Reference

 https://www.w3schools.com/python/python_try_except.asp
 https://docs.python.org/3/tutorial/errors.html

By Nishan Saliya IIT 31


Any Questions?

By Nishan Saliya IIT 32

You might also like