Embed presentation








![© SkillBrew http://skillbrew.comThrowing an exception9def avg(seq):result = 0for val in seq:result += convert(val)return result/len(seq)def convert(val):try:val = int(val)except ValueError:raise ValueError('val type is not int')return valprint avg([1, 2, 4, 5])Output:3](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonprogrammingessentials-m21-exceptionhandling-140819043210-phpapp01%2f75%2fPython-Programming-Essentials-M21-Exception-Handling-9-2048.jpg&f=jpg&w=240)
![© SkillBrew http://skillbrew.comThrowing an exception10print avg([1, 'two', 4, 5])Output:Traceback (most recent call last):File "exceptions1.py", line 15, in <module>print avg([1, 'two', 4, 5])File "exceptions1.py", line 4, in avgresult += convert(val)File "exceptions1.py", line 11, in convertraise ValueError('val type is not int')ValueError: val type is not int](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonprogrammingessentials-m21-exceptionhandling-140819043210-phpapp01%2f75%2fPython-Programming-Essentials-M21-Exception-Handling-10-2048.jpg&f=jpg&w=240)









This document discusses exception handling in Python. It explains that exceptions are errors that occur during program execution and can be handled using try, except, and raise keywords. The document covers throwing exceptions, catching exceptions using try/except blocks, propagating exceptions, using try/except/else and try/except/finally, and some common built-in Python exceptions. It also provides an example of defining a custom exception class.








![© SkillBrew http://skillbrew.comThrowing an exception9def avg(seq):result = 0for val in seq:result += convert(val)return result/len(seq)def convert(val):try:val = int(val)except ValueError:raise ValueError('val type is not int')return valprint avg([1, 2, 4, 5])Output:3](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonprogrammingessentials-m21-exceptionhandling-140819043210-phpapp01%2f75%2fPython-Programming-Essentials-M21-Exception-Handling-9-2048.jpg&f=jpg&w=240)
![© SkillBrew http://skillbrew.comThrowing an exception10print avg([1, 'two', 4, 5])Output:Traceback (most recent call last):File "exceptions1.py", line 15, in <module>print avg([1, 'two', 4, 5])File "exceptions1.py", line 4, in avgresult += convert(val)File "exceptions1.py", line 11, in convertraise ValueError('val type is not int')ValueError: val type is not int](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonprogrammingessentials-m21-exceptionhandling-140819043210-phpapp01%2f75%2fPython-Programming-Essentials-M21-Exception-Handling-10-2048.jpg&f=jpg&w=240)







