How to catch IndentationError Exception in python?

Manogna
Published on 06-Dec-2017 17:24:52
A IndentationError occurs any time the parser finds source code that does not follow indentation rules. We can catch it when importing a module, since the module will be compiled on first import. You can't catch it in the same module that contains the try/except block, because with this exception, ... Read More

How to catch ZeroDivisionError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:56:16
When zero shows up in the denominator of a division operation, a ZeroDivisionError is raised.We re-write the given code as follows to handle the exception and find its type.Exampleimport sys try: x = 11/0 print x except Exception as e: print sys.exc_type print eOutput<type 'exceptions.ZeroDivisionError'> integer division or modulo by ... Read More

How to catch FloatingPointError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:55:11
FloatingPointError is raised by floating point operations that result in errors, when floating point exception control (fpectl) is turned on. Enabling fpectl requires an interpreter compiled with the --with-fpectl flag.The given code is rewritten as follows to handle the exception and find its type.Exampleimport sys import math import fpectl try: ... Read More

How to catch StandardError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:52:30
There is the Exception class, which is the base class for StopIteration, StandardError and Warning. All the standard errors are derived from StandardError. Some standard errors like ArithmeticErrror, AttributeError, AssertionError are derived from base class StandardError.When an attribute reference or assignment fails, AttributeError is raised. For example, when trying to ... Read More

How to catch StopIteration Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:50:09
When an iterator is done, it’s next method raises StopIteration. This exception is not considered an error.We re-write the given code as follows to catch the exception and know its type.Exampleimport sys try: z = [5, 9, 7] i = iter(z) print i print i.next() print i.next() print i.next() print ... Read More

How to catch SystemExit Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:48:06
In python documentation, SystemExit is not a subclass of Exception class. BaseException class is the base class of SystemExit. So in given code, we replace the Exception with BaseException to make the code workExampletry: raise SystemExit except BaseException: print "It works!"OutputIt works!The exception inherits from BaseException instead of StandardError or ... Read More

How to catch ImportError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:45:04
ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised.If a module does not exist.Exampleimport sys try:     from exception import myexception except Exception as e:     print e     print sys.exc_typeOutputNo ... Read More

How to catch NotImplementedError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:42:06
User-defined base classes can raise NotImplementedError to indicate that a method or behavior needs to be defined by a subclass, simulating an interface. This exception is derived from RuntimeError. In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method.Exampleimport sys ... Read More

How to catch OSError Exception in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 16:40:07
OSError serves as the error class for the os module, and is raised when an error comes back from an os-specific function.We can re-write the given code as follows to handle the exception and know its type.#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except ... Read More

How to handle Python exception in Threads?

Rajendra Dharmkar
Published on 06-Dec-2017 16:35:29
The given code is rewritten to catch the exceptionimport sys import threading import time import Queue def thread(args1, stop_event, queue_obj): print "start thread" stop_event.wait(12) if not stop_event.is_set(): try: raise Exception("boom!") except Exception: queue_obj.put(sys.exc_info()) pass try: queue_obj = Queue.Queue() t_stop = threading.Event() t = threading.Thread(target=thread, args=(1, t_stop, queue_obj)) t.start() time.sleep(15) print ... Read More
Advertisements