Module01 Wk02b Exceptions
Module01 Wk02b Exceptions
– Exceptions
Networking for Software Developer
Narendra Pershad
Agenda
• Errors
• Why is it necessary?
• Examples
Errors
• Errors prevents your program must running as expected (or as you hope)
• There are basically two kinds of errors that you might get
• Syntax Errors
• Normally comes from coding mistakes
• Exceptions
• Normally come from some abnormalities in execution
Syntax Errors
• The parser tokenizes the source code and then parses according to the grammar of the language.
• The parser prints the offending line with an arrow indicating the earliest point where the error was detected.
• This is not an issue for complied languages such C++, C, java and C# where all the syntax are caught before
the executable is built.
Exception
• If you don’t handle the exception then the program will terminate.
Full Syntax
try:
pass #statements that may cause exception
except:
pass #processed if that is an exception
else: #optional
pass #processed if that is no exception
finally: #optional
pass #guarantee to be processed even if there is no
exception
Simple Syntax
try:
try:
try:
if name == 'Narendra’:
raise Exception('Narendra is evil!’) #raise an exception
else:
print(f'Hello {name}')
except Exception as e:
print(e)
Summary
• There are two kinds of errors that a user might encounter
• Syntax Errors
• Exceptions