Chapter-3
What is Exception?
Python provides two very important features to handle any unexpected error in
your Python programs and to add debugging capabilities in them:
• Exception Handling:
• Assertions:
What is Exception?
• An exception is an event, which occurs during the execution of a program, that
disrupts the normal flow of the program's instructions.
• In general, when a Python script encounters a situation that it can't cope with, it
raises an exception. An exception is a Python object that represents an error.
• When a Python script raises an exception, it must either handle the exception
immediately otherwise it would terminate and come out.
Built in Exception in Python
The raise Statement
• The raise statement can be used to throw an exception.
• The syntax of raise statement is:
raise exception-name[(optional argument)]
The assert Statement
• An assert statement in Python is used to test an expression in the
program code. If the result after testing comes false, then the
exception is raised.
• This statement is generally used in the beginning of the function or
after a function call to check for valid input.
• The syntax for assert statement is:
assert Expression[,arguments]
On encountering an assert statement, Python evaluates the expression
given immediately after the assert keyword. If this expression is false,
an AssertionError exception is raised which can be handled like any
other exception.
Example of assert statement
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf
ch-3-exception-handling.pdf

ch-3-exception-handling.pdf

  • 1.
  • 5.
    What is Exception? Pythonprovides two very important features to handle any unexpected error in your Python programs and to add debugging capabilities in them: • Exception Handling: • Assertions: What is Exception? • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. • In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error. • When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.
  • 8.
  • 10.
    The raise Statement •The raise statement can be used to throw an exception. • The syntax of raise statement is: raise exception-name[(optional argument)]
  • 11.
    The assert Statement •An assert statement in Python is used to test an expression in the program code. If the result after testing comes false, then the exception is raised. • This statement is generally used in the beginning of the function or after a function call to check for valid input. • The syntax for assert statement is: assert Expression[,arguments] On encountering an assert statement, Python evaluates the expression given immediately after the assert keyword. If this expression is false, an AssertionError exception is raised which can be handled like any other exception.
  • 12.