Exception Handling in Python with Example

Exception Handling in Python :

Definition: 
  • Exception handling allows the program to catch and handle runtime errors without crashing. It is done using try, except, and optionally finally blocks.
  • Important Keywords: try, except, else, finally.

Types of Errors:

Compile-time Error:

Definition: 
  • Occurs during code compilation before execution. Syntax errors, like misspelling a function, lead to compile-time errors.

Example:





Logical Error:

Definition: 
  • The code runs successfully but produces incorrect results due to a logic flaw in the program.
Example:




Runtime Error:
Definition: 
  • Occurs while the program is running, usually due to invalid input, division by zero, etc.
Example:




Exception Handling with try and except:

Basic Exception Handling:

Definition: 
  • Catches any exception without specifying the type.
Example Program:



Output :



Post a Comment

0 Comments