Python Exception Handling



Exception

To understand the first exception, we must know about the error in the program. Program errors can be classified into two types.

  • Compile Time Errors
  • Run-Time Errors

Compile Time Errors

Errors caught during the compiled time are called compile-time errors. Compile-time errors include library reference, syntax error, or incorrect class import.

Run-Time Errors

An error that occurs during a program's run time is called a run time error.

They are also known as exceptions. Hence we can say that exception is a runtime error that occurs because of a user's mistake.


Reasons of Exception

Mismatched Input: Assuming we are entering our name instead of age, we will make an exception because the age data type is an integer and the name will be a string.

Divide by zero exception: When the number is divided by zero the output will be undefined(infinite).

IndentationError: If incorrect indentation is given.

The file does not exist: Suppose we are reading a text file demo.text and it does not exist in the file system, there is an exception.


Exception Handling

The process of managing an exception is called exception handling. Three main keywords are used to solve the problem of exception.

  • try block
  • except block 
  • finally block 

try block: This is where the actual code is written and the exception occurs. When the code leads to an error, that error/exception will get caught inside the except block.

except block: The purpose is to catch the error except the block and handle the exception situation. We can have multiple exceptions to manage different types of exceptions and perform different actions when exceptions occur.

finally block: This block is executed or an exception occurs.


Syntax 

try:
    pass
except:
    pass
finally:
    pass

Example 1

try:
    name = "Denny"
    print("My name is ", name)
except:
    print("Exception occur")

Output

My name is  Denny

This Example is without Exception.


Example 2

try:
    print("My name is ", name)
except:
    print("Exception occur")

Output

Exception occur

Syntax of try,  except, else block

try:
    pass
except:
    pass
else:
    pass

Example 1

try:
    name = "Denny"
    print("My name is ", name)
except:
    print("Exception occur")
else:
    print("Else Block Execute")

Output

My name is  Denny
Else Block Execute

Example 2

try:
    x = 10
    y = 0
    z = x/y
    print("Value of Z is", z)
except:
    print("Zero division Error Occur")
else:
    print("Else Block Execute")

Output

Zero division Error Occur

Syntax of try,  except, else, finally block

try:
    pass
except:
    pass
else:
    pass
finally:
    pass

Example 1

try:
    x = 10
    y = 2
    z = x/y
    print("Value of Z is", z)
except:
    print("Zero division Error Occur")
else:
    print("Else Block Execute")
finally:
    print("This is Finally Block")

Output

Value of Z is 5.0
Else Block Execute
This is Finally Block

Example 2

try:
    x = 10
    y = 0
    z = x/y
    print("Value of Z is", z)
except:
    print("Zero division Error Occur")
else:
    print("Else Block Execute")
finally:
    print("This is Finally Block")

Output

Zero division Error Occur
This is Finally Block