Fish Touching🐟🎣

Python Error and Exceptions

Apr 3, 2023

C++ error handling
JavaScript Error Handling
Src

# Exceptions Handling

An exception is thrown when a fundamental assumption of the current code block is found to be false.
try, catch, and finally statements enable the use of a resource, catching any possible exceptions and then releasing the resource in the final block, making sure that there are no memory leaks.

# Try-Except Clause

Built-in Exceptions

>>> while True:
...     try:
...         x = int(input("Please enter a number: "))
...         break
...     except ValueError:
...         print("Oops!  That was no valid number.  Try again...")

The  try statement works as follows.

The  try …  except statement has an optional else clause, which, when present, must follow all except clauses.
It is useful for code that must be executed if the try clause does not raise an exception. For example:

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except OSError:
        print('cannot open', arg)
    else:
        print(arg, 'has', len(f.readlines()), 'lines')
        f.close()

The use of the else clause is better than adding additional code to the  try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement

# Example

>>> def this_fails():
...     x = 1/0
...
>>> try:
...     this_fails()
... except ZeroDivisionError as err:
...     print('Handling run-time error:', err)
...
Handling run-time error: division by zero

# Exception Chaining

Src

>>> def func():
...     raise ConnectionError
...
>>> try:
...     func()
... except ConnectionError as exc:
...     raise RuntimeError('Failed to open database') from exc
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in func
ConnectionError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Failed to open database

# Finally Clause

Src
The finally clause will execute as the last task before the  try statement completes. The finally clause runs whether or not the try statement produces an exception.

>>> def bool_return():
...     try:
...         return True
...     finally:
...         return False
...
>>> bool_return()
False

>>> def divide(x, y):
...     try:
...         result = x / y
...     except ZeroDivisionError:
...         print("division by zero!")
...     else:
...         print("result is", result)
...     finally:
...         print("executing finally clause")
...
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'

As you can see, the  finally clause is executed in any event. The  TypeError raised by dividing two strings is not handled by the  except clause and therefore re-raised after the finally clause has been executed.
In real world applications, the  finally clause is useful for releasing external resources (such as files or network connections), regardless of whether the use of the resource was successful.

# User-defined Exceptions

Src
Programs may name their own exceptions by creating a new exception class (see  Classes for more about Python classes).
Exceptions should typically be derived from the  Exception class, either directly or indirectly.
More on Python OOP