Exception Handling in Python For ML

Ravish Kumar
EnjoyAlgorithms
Published in
4 min readFeb 26, 2024

What are exceptions?

Computer programs can be syntactically correct but still fail during runtime (execution) because of some fundamental logical issues. The errors detected during runtime are known as exceptions. For example:

a = 1/0

The statement above is syntactically correct, but we can quickly determine that division by zero is impossible with mathematical logic. Computer programs are smart enough to catch these exceptions and will produce errors. For the above statement in Python, it will make ZeroDivisionError, shown below:


Traceback (most recent call last)
Cell In [31], line 1
----> 1 a = 1/0

ZeroDivisionError: division by zero

Some predefined exceptions are available in Python, and the most popular ones are ZeroDivisionError, NameError, and TypeError. Examples of these can be seen in the code below:

>>> a = 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

>>> b = alpha*10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'alpha' is not defined

>>> c = 'age' + 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

List of predefined exceptions in Python?

We can get a list of all the predefined illegal operations in Python programming language like this:

print(dir(locals()['__builtins__']))

['ArithmeticError', 'AssertionError',
'AttributeError', 'BaseException', 'BlockingIOError',
'BrokenPipeError', 'BufferError', 'BytesWarning',
'ChildProcessError', 'ConnectionAbortedError',
'ConnectionError', 'ConnectionRefusedError',
'ConnectionResetError', 'DeprecationWarning',
'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'False', 'FileExistsError',
'FileNotFoundError', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError',
'ImportError', 'ImportWarning', 'IndentationError',
'IndexError', 'InterruptedError', 'IsADirectoryError',
'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError',
'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError',
'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError',
'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError',
'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError',
'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',
'ZeroDivisionError', '__build_class__', '__debug__', '__doc__',
'__import__', '__loader__', '__name__', '__package__', '__spec__',
'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray',
'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate',
'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr',
'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int',
'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals',
'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord',
'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round',
'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum',
'super', 'tuple', 'type', 'vars', 'zip']

Errors and Exceptions

Computer programs may fail because of many issues like fundamental logical flaws, infinite loops, missing base conditions in recursion, or even syntax errors. Out of all these issues, flaws or failures coming from fundamental predefined logical failures are known as exceptions. These exceptions can easily be detected during the execution of a program and can be handled as well, which we call exception handling.

Note: Although programmers can handle errors using exceptional handling techniques, it is not advisable to do that.

Exception Handling in Python

Now that we know exceptions terminate the execution of programs, we need to handle them properly so that other processes are not affected. We use “try and except” statements in Python to handle them. The syntax for the try-except block can be seen below:

try:
# code that can create exceptions

except:
# code to execute if there is any exception

Example 1: Handline ZeroDivisionError exception using try-except block

try:
a = 1
b = 0
c = a/b

except:
print("Denominator can not be 0")

## Denominator can not be 0

Example 2: Handling the particular exception in multiple except statements

Here, we have used variables “d” and “e” and did not define them in earlier statements. It will produce NameError; hence, that except statement will be called.

try:
a = 1
b = 0
c = d/e

except ZeroDivisionError:
print("Denominator can not be 0")

except NameError:
print("Some variable is not defined")

## Some variable is not defined

Example 3: Try-except-else statements

While catching the particular statements, we sometimes need some notification that our try statement worked without any exceptions. In that case, to get notified that we have successfully avoided any exceptions, we can use a try-except-else statement like this:

try:
a = 10
b = 2
c = a/b

except NameError:

print("Undefined variable is used!!")

else:
print("Division Successful.")

## Division Successful.

Example 4: Try-except-else-finally statements

After getting notified and coming out of this try-except block, we can use the final statement in the last if we need to execute one final program. For example, if we have opened the file, we finally need to close it after passing the try-except statement like this:

try:
my_file = open('0001-Copy1.1999-12-10.farmer.ham.txt', "r")
file_content = my_file.read()

except NameError:

print("Undefined variable is used!!")

else:
print("Content read successfully.")

finally:
my_file.close()
print("File is closed.")

##Content read successfully.
## File is closed.

Use Cases of Exception Handling

  • Programmers deal with a lot of codebase while working on a large software of ML applications. Exception handling makes the program debugging easy and efficiently catches which line creates the issue.
  • With exception handling, we can stop the abrupt termination of programs and keep continuing the execution of the rest of the statements.
  • While implementing the machine learning model, we need to import various libraries and execute these models on remote servers. Some libraries may not be installed on the remote server. We can avoid the execution of those libraries with the help of try-except statements.

Conclusion

Computer programs fail to execute because of some predefined logical flaws, which are known as exceptions. They can terminate the execution of programs abruptly and need to be handled. We use a try-except statement block in Python to catch and handle the exceptions separately. This helps debug large codes easily and maintain smooth execution in case of small mistakes.

Enjoy Learning.

16 Week Live Project-Based ML Course: Admissions Open

--

--

Ravish Kumar
EnjoyAlgorithms

Deep Learning Engineer@Deeplite || Curriculum Leader@ enjoyalgorithms.com || IIT Kanpur || Entrepreneur || Super 30