How to resolve the algorithm Exceptions step by step in the Python programming language
How to resolve the algorithm Exceptions step by step in the Python programming language
Table of Contents
Problem Statement
This task is to give an example of an exception handling routine and to "throw" a new exception.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Exceptions step by step in the Python programming language
-
The first statement of your code defines a custom exception called
SillyError
, which inherits from the built-inException
class. The__init__()
method takes a single argument,args
, and stores it in theself.args
instance variable. -
The second statement defines another custom exception called
MyInvalidArgument
, which inherits from theValueError
class. This class does not define any additional behavior beyond what is inherited fromValueError
. -
The third statement defines a function called
spam()
that raises aSillyError
exception without any arguments. This is equivalent to raising aSillyError()
exception. -
The fourth statement defines a function called
spam()
that raises aSillyError
exception with the argument 'egg'. This is equivalent to raising aSillyError('egg')
exception. -
The fifth statement defines a function called
spam()
that raises aSillyError
exception with the argument 'egg'. This is equivalent to raising aSillyError('egg')
exception. -
The sixth statement defines a
try/except
block that attempts to call thefoo()
function. If thefoo()
function raises aSillyError
exception, the exception will be caught and theprint(se.args)
statement will be executed, wherese
is the caught exception. Thebar()
function will then be called. If no exception is raised by thefoo()
function, theelse
block will be executed and thequux()
function will be called. Thefinally
block will always be executed, regardless of whether an exception was raised or not, and thebaz()
function will be called. -
The seventh statement defines a
try/except
block that attempts to call thefoo()
function. If thefoo()
function raises aSillyError
exception, the exception will be caught and theprint(se.args)
statement will be executed, wherese
is the caught exception. Thebar()
function will then be called. If no exception is raised by thefoo()
function, theelse
block will be executed and thequux()
function will be called. Thefinally
block will always be executed, regardless of whether an exception was raised or not, and thebaz()
function will be called.
Source code in the python programming language
import exceptions
class SillyError(exceptions.Exception):
def __init__(self,args=None):
self.args=args
class MyInvalidArgument(ValueError):
pass
def spam():
raise SillyError # equivalent to raise SillyError()
def spam():
raise SillyError, 'egg' # equivalent to raise SillyError('egg')
def spam():
raise SillyError('egg')
try:
foo()
except SillyError, se:
print se.args
bar()
else:
# no exception occurred
quux()
finally:
baz()
try:
foo()
except SillyError as se:
print(se.args)
bar()
else:
# no exception occurred
quux()
finally:
baz()
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the APL programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the BASIC programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Befunge programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the C++ programming language
You may also check:How to resolve the algorithm Currying step by step in the Sidef programming language