Wednesday, May 29, 2024

What is Try Except in Python and How can we Use it?

The try except statement can handle exceptions. Exceptions may happen when you run a program.Exceptions are errors that happen during execution of the program. Python won’t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.An abrupt exit is bad for both the end user and developer.Instead of an emergency halt, you can use a try except statement to properly deal with the problem. An emergency halt will happen if you do not properly handle exceptions.

Python and Machine Learning Rectangle

What are exceptions in Python?

Python has built-in exceptions which can output an error. If an error occurs while running the program, it’s called an exception.

If an exception occurs, the type of exception is shown. Exceptions needs to be dealt with or the program will crash. To handle exceptions, the try-catch block is used.

Some exceptions you may have seen before are FileNotFoundErrorZeroDivisionError or ImportError but there are many more.

All exceptions in Python inherit from the class BaseException. If you open the Python interactive shell and type the following statement it will list all built-in exceptions:

 >>> dir(builtins)

The idea of the try-except clause is to handle exceptions (errors at runtime). The syntax of the try-except block is:

    <do something>
except Exception:
    <handle the error>

The idea of the try-except block is this:

  • try: the code with the exception(s) to catch. If an exception is raised, it jumps straight into the except block.
  • except: this code is only executed if an exception occured in the try block. The except block is required with a try block, even if it contains only the pass statement.

It may be combined with the else and finally keywords.

  • else: Code in the else block is only executed if no exceptions were raised in the try block.
  • finally: The code in the finally block is always executed, regardless of if a an exception was raised or not.

Grab the opportunity to learn Python with Entri! Click Here

Catching Exceptions in Python

The try-except block can handle exceptions. This prevents abrupt exits of the program on error. In the example below we purposely raise an exception.

    1 / 0
except ZeroDivisionError: 
    print('Divided by zero')

print('Should reach here')

After the except block, the program continues. Without a try-except block, the last line wouldn’t be reached as the program would crash.

 $ python3

Divided by zero
Should reach here

In the above example we catch the specific exception ZeroDivisionError. You can handle any exception like this:

    print('Something went wrong')

print('Should reach here')

You can write different logic for each type of exception that happens:

    # your code here
except FileNotFoundError: 
    # handle exception 
except IsADirectoryError:
    # handle exception
    # all other types of exceptions

print('Should reach here')


Let us do a real world example of the try-except block.

The program asks for numeric user input. Instead the user types characters in the input box. The program normally would crash. But with a try-except block it can be handled properly.

The try except statement prevents the program from crashing and properly deals with it.

    x = input("Enter number: ")
    x = x + 1
    print("Invalid input")

Entering invalid input, makes the program continue normally:

try except

The try except statement can be extended with the finally keyword, this will be executed if no exception is thrown:

    print("Valid input.")

The program continues execution if no exception has been thrown.

There are different kinds of exceptions: ZeroDivisionError, NameError, TypeError and so on. Sometimes modules define their own exceptions.

The try-except block works for function calls too:

def fail():
    1 / 0

    print('Exception occured')

print('Program continues')


This outputs:

 $ python3

Exception occured
Program continues

Learn to code from industry experts! Enroll here

try finally

A try-except block can have the finally clause (optionally). The finally clause is always executed.
So the general idea is:

    <do something>
except Exception:
    <handle the error>

For instance: if you open a file you’ll want to close it, you can do so in the finally clause.

    f = open("test.txt")
    print('Could not open file')

print('Program continue')

try else

The else clause is executed if and only if no exception is raised. This is different from the finally clause that’s always executed.

    x = 1
    print('Failed to set x')
    print('No exception occured')
    print('We always do this')


 No exception occured
 We always do this

You can catch many types of exceptions this way, where the else clause is executed only if no exception happens.

except SyntaxError:
    print('Fix your syntax')
except TypeError:
    print('Oh no! A TypeError has occured')
except ValueError:
    print('A ValueError occured!')
except ZeroDivisionError:
    print('Did by zero?')
    print('No exception')
    print('Ok then')

Raise Exception

Exceptions are raised when an error occurs. But in Python you can also force an exception to occur with the keyword raise.

Any type of exception can be raised:

>>> raise MemoryError("Out of memory")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: Out of memory
>>> raise ValueError("Wrong value")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Wrong value

Built-in exceptions

A list of Python’s Built-in Exceptions is shown below. This list shows the Exception and why it is thrown (raised).

Exception Cause of Error
AssertionError if assert statement fails.
AttributeError if attribute assignment or reference fails.
EOFError if the input() functions hits end-of-file condition.
FloatingPointError if a floating point operation fails.
GeneratorExit Raise if a generator’s close() method is called.
ImportError if the imported module is not found.
IndexError if index of a sequence is out of range.
KeyError if a key is not found in a dictionary.
KeyboardInterrupt if the user hits interrupt key (Ctrl+c or delete).
MemoryError if an operation runs out of memory.
NameError if a variable is not found in local or global scope.
NotImplementedError by abstract methods.
OSError if system operation causes system related error.
OverflowError if result of an arithmetic operation is too large to be represented.
ReferenceError if a weak reference proxy is used to access a garbage collected referent.
RuntimeError if an error does not fall under any other category.
StopIteration by next() function to indicate that there is no further item to be returned by iterator.
SyntaxError by parser if syntax error is encountered.
IndentationError if there is incorrect indentation.
TabError if indentation consists of inconsistent tabs and spaces.
SystemError if interpreter detects internal error.
SystemExit by sys.exit() function.
TypeError if a function or operation is applied to an object of incorrect type.
UnboundLocalError if a reference is made to a local variable in a function or method, but no value has been bound to that variable.
UnicodeError if a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError if a Unicode-related error occurs during encoding.
UnicodeDecodeError if a Unicode-related error occurs during decoding.
UnicodeTranslateError if a Unicode-related error occurs during translating.
ValueError if a function gets argument of correct type but improper value.
ZeroDivisionError if second operand of division or modulo operation is zero.

Learn Coding in your Language! Enroll Here!

 User-defined Exceptions

Python has many standard types of exceptions, but they may not always serve your purpose.
Your program can have your own type of exceptions.

To create a user-defined exception, you have to create a class that inherits from Exception.

class LunchError(Exception):

raise LunchError("Programmer went to lunch")

You made a user-defined exception named LunchError in the above code. You can raise this new exception if an error occurs.

Outputs your custom error:

 $ python3
Traceback (most recent call last):
  File “”, line 5, in 
    raise LunchError(“Programmer went to lunch”)
main.LunchError: Programmer went to lunch

Your program can have many user-defined exceptions. The program below throws exceptions based on a new projects money:

class NoMoneyException(Exception):

class OutOfBudget(Exception):

balance = int(input("Enter a balance: "))
if balance < 1000:
    raise NoMoneyException
elif balance > 10000:
    raise OutOfBudget

Here are some sample runs:

 $ python3
    Enter a balance: 500
    Traceback (most recent call last):
      File “”, line 10, in 
        raise NoMoneyException
 $ python3
    $ python3
    Enter a balance: 100000
    Traceback (most recent call last):
      File “”, line 12, in 
        raise OutOfBudget

It is a good practice to put all user-defined exceptions in a separate file ( or This is common practice in standard modules too.

Python and Machine Learning Square

Related Posts

Next Post