Raising the SystemExitexception is a generic way to leave the Python interpreter.C:\Program Files\Python>python
The next example demonstrates how you can trap the SystemExitexception.>>> try:
The sys.exit() function raises an exception SystemExitthat, if not caught, causes the thread to exit silently.>>> import sys
Raising an Exception to Leave Nested Loops
Sometimes you are so deeply involved in your data structures that you only want to get out of all your nested loops quickly. Normally, you would have to use break for each level of interaction. The next example demonstrates how to handle this situation by using exceptions.>>>ExitLoop = "ExitLoop"
Raising String Exceptions
Older versions used to support only strings for both Python standard exceptions and user-defined exceptions.>>>NetworkError = "NetworkError"
Nowadays, Python supports both strings and exception classes. There are costs to using class exceptions because they must be instantiated to be caught. Note that most people don't use exceptions to control the flow of their program, so they don't occur much.
However, classes give you much more flexibility to generalize the type of error that you want to catch.
Instancing an Exception Class
Every time an exception is raised, an instance of the exception class is created. The next syntax demonstrates how to catch a class instance in your program.try:
The instance variable is an instance of the raised exception. Therefore, it inherits attributes from the exception class.
Each instance has an attribute called argsthat returns the error string in a tuple format.>>> try:
Particularly, the EnvironmentErrorexception has a 2-tuple or 3-tuple structure that can be translated as (error number, string error message, and an optional filename).>>> try:
When the instance belongs to a SyntaxErrorclass exception, four special attributes are also returned: filename, lineno, offset, and text.>>> try:
Debugging Your Code
Exceptions are very good for helping to debug your code. You can use the assert command to raise a debugging exception that transports a message to your exception handling code.
The syntax is assert <TestStatement> [,argument] This command raises an Assertion Error exception whenever <Test Statement>evaluates to false. For example>>>def divide (a,b):
__debug__ is a built-in name and has its value set to true by default. To set __debug__ to false, it is necessary to change the interpreter to run in optimized mode.c:\>python -O
Currently, Python's command-line option -X turns all standard exceptions into strings. Version 1.6 is expected to have this option removed, and make all standard exceptions into classes. User code that deals with string exceptions will still be supported, but not encouraged.
Python Related Interview Questions
|Perl Scripting Interview Questions||C++ Interview Questions|
|PHP Interview Questions||C Interview Questions|
|Ruby on Rails Interview Questions||Ruby Interview Questions|
|Django Interview Questions||Lisp programming Interview Questions|
|R Programming language Interview Questions||wxPython Interview Questions|
|Python Automation Testing Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.