4. Basic Error Handling (try / except)
Used to handle runtime errors without crashing the program.
Basic Syntax
Example
Catch Specific Exceptions
Multiple Exceptions
else
Runs only if no exception occurs.
finally
Always executes.
Raising Exceptions
Common Built-in Exceptions
Quick Revision
Error Handling
try→ Code that may fail.except→ Handle exceptions.else→ Runs when no exception occurs.finally→ Always runs.raise→ Manually throw an exception.- Catch specific exceptions whenever possible instead of using a bare
except.