Sei sulla pagina 1di 2

Errors:

1. Syntaxical Errors
*are detected at COmpile Time
*are syntaxical / format errors
*if code contains syntaxical errors then we cannot run the code

2. Run Time Errors / Exceptions


*are detected at Run Time
*are logical errors or system flaws
*if code contains run time errors then we can still run the code
*but the code crashes on exception detection
*crash -> untimely application exit

Exception Handling: basic idea is crash prevention


Tasks:
*we detect the Exception/s
*on detection, we take corrective actions

We have built in exceptions in Python as well as user defined exceptions.

ZeroDivisionError
ValueError
IOError
ModuleNotFound
ImportError

All built in exceptions are built in classes...

Exception Handling Syntax:


-------------------------------------
try:
---
error prone statements
statements dependent on the error prone statements
---
except ExceptionName:
take corrective action for that exception
show error message
except ExceptionName:
take corrective action for that exception
show error message
except:
optional - generic except block
any exception left over will be caught in the generic except block
has to be written only after all except blocks
else:
optional
this would be executed when there is not exception
finally:
optional
this block would always be executed | used for closure coding
-------------------------------------
*the except block is not executed till the time its corresponding exception occurs
in the try block.
*one except block represents one type of exception
*a try block can have multiple except blocks.
*except cannot exist without try
--------------------------------------
try:
---
error prone statements
statements dependent on the error prone statements
---
except ExceptionName:
take corrective action for that exception
except ExceptionName:
take corrective action for that exception
except:
optional - generic except block
any exception left over will be caught in the generic except block
has to be written only after all except blocks
else:
optional
this would be executed when there is not exception
finally:
optional
this block would always be executed | used for closure coding
---------------------------------------------------

Potrebbero piacerti anche