Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Exception handling in python
  5. Exception handling

Exception handling

Errors and exceptions are similar but different terms.

While error represents, any bug in the code that disrupt running of the program or cause improved output.

An exception refers to any irregular situation occurring during run time, which we have no control on.

Errors in a program can be fixed by making corrections in the code, fixing exceptions is not that simple.

Exception in a program as a situation that occurs during runtime and programmer has no control on it.

A good program is one that has a code that handles an exception well i.e., in case an exception occurs, then the exception handling code written for it, will take over and do the necessary action.

Exception handling in python involves the use of try and except clauses wherein the code that may generate an exception is written in the try block and the code for handling the exception when the exception is raised, written in except block.

syntax:-

try:
    #write here the code that may generate an exception
except:
    #write code here about what to do if the exception has occurred.

This is all about exception handling in python.

Was this article helpful to you? Yes No

How can we help?