Header Ads

Exception Handling in Python

Exception handling in python one of the features of OOPS.It is mainly designed to handled runtime errors and the exceptions that corrupt the flow of the program.
Exception Handling in Python
There are 5 keywords that are used in exception handling: 


There may be some block of code that may get an error during the runtime so we run that code inside the try block and if the code gets interrupted by any errors, it leaves the block and goes to the except block. 

  a = 3
  b = 0
  print("Zero Division Error")
 Output:Zero Division Error  
Explanation: In the try block, the number is divided by zero which faces an exception and moves to the except. Here, no specific error type is found and prints the message passed in the except block.

Handling Common Errors
Arithmetic error - Occurs when the arithmetic calculations fail.
Floating-point error - Occurs when the calculations of floating-point values fail.
Import error - Occurs when the import file is wrong or mismatched.
IO error - Occurs when the input or output acts as excepted.
Key error - Occurs when the key is not found or mismatched in the dictionary.
Name error - Occurs when the variable name is not found or mismatched.
Syntax error - Occurs when the syntax for the logic used is wrong.
Value error - Occurs when the object created to store the value does not exist or fails to store the value.
Zero division error - Occurs when the number is divided by zero.

No comments:

Powered by Blogger.