Found 27104 Articles for Server Side Programming

How to catch SystemExit Exception in Python?

Rajendra Dharmkar
Updated on 12-Feb-2020 10:39:46

2K+ Views

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 Exception so that it is not accidentally caught by code that catches Exception.We would rather write the code this wayExampletry: raise SystemExit except SystemExit: print "It works!"OutputIt works!

How to catch StopIteration Exception in Python?

Rajendra Dharmkar
Updated on 12-Feb-2020 10:40:43

1K+ Views

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 i.next() except Exception as e: print e print sys.exc_type Output 5 9 7

How to catch StandardError Exception in Python?\\\

Rajendra Dharmkar
Updated on 12-Feb-2020 10:41:26

389 Views

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 reference an attribute that does not exist:We rewrite the given code and catch the exception and know it type.Exampleimport sys try: class Foobar: def __init__(self): self.p = 0 f = Foobar() print(f.p) print(f.q) except Exception as e: print e print sys.exc_type print 'This is an example of StandardError exception'Output0 Foobar ... Read More

How to catch FloatingPointError Exception in Python?

Rajendra Dharmkar
Updated on 12-Feb-2020 10:42:09

1K+ Views

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: print 'Control off:', math.exp(700) fpectl.turnon_sigfpe() print 'Control on:', math.exp(1000) except Exception as e: print e print sys.exc_type OutputControl off: 1.01423205474e+304 Control on: in math_1

How to catch ZeroDivisionError Exception in Python?

Rajendra Dharmkar
Updated on 12-Feb-2020 10:42:53

434 Views

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 integer division or modulo by zero

How to catch ValueError using Exception in Python?

Manogna
Updated on 12-Jun-2020 07:56:10

274 Views

A ValueError is used when a function receives a value that has the right type but an invalid value.The given code can be rewritten as follows to handle the exception and find its type.Exampleimport sys try: n = int('magnolia') except Exception as e: print e print sys.exc_typeOutputinvalid literal for int() with base 10: 'magnolia'

How to catch LookupError Exception in Python?

Manogna
Updated on 12-Feb-2020 10:54:12

1K+ Views

LookupError Exception is the Base class for errors raised when something can’t be found. The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError.An IndexError is raised when a sequence reference is out of range.The given code is rewritten as follows to catch the exception and find its typeExampleimport sys try: foo = [a, s, d, f, g] print foo[5] except IndexError as e: print e print sys.exc_typeOutputC:/Users/TutorialsPoint1~.py list index out of range

How to catch EnvironmentError Exception in Python?

Manogna
Updated on 12-Feb-2020 10:53:26

326 Views

EnvironmentError is the base class for errors that come from outside of Python (the operating system, filesystem, etc.). EnvironmentError Exception is a subclass of the StandarError class. It is the base class for IOError and OSError exceptions. It is not actually raised unlike its subclass errors like IOError and OSError.Any example of an IOError or OSError should be an example of Environment Error as well.Exampleimport sys try: f = open ( "JohnDoe.txt", 'r' ) except Exception as e: print e print sys.exc_typeOutput[Errno 2] No such file or directory: 'JohnDoe.txt'

How to catch TypeError Exception in Python?

Manogna
Updated on 12-Feb-2020 10:51:23

551 Views

TypeErrors are caused by combining the wrong type of objects, or calling a function with the wrong type of object.Exampleimport sys try : ny = 'Statue of Liberty' my_list = [3, 4, 5, 8, 9] print  my_list + ny except TypeError as e: print e print sys.exc_typeOutputcan only concatenate list (not ""str") to list

How to catch IndentationError Exception in python?

Manogna
Updated on 12-Feb-2020 10:43:35

1K+ Views

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, Python won't be able to finish compiling the module, and no code in the module will be run.We rewrite the given code as follows to handle the exceptionExampletry: def f(): z=['foo', 'bar'] for i in z: if i == 'foo': except IndentationError as e: print eOutput"C:/Users/TutorialsPoint1/~.py", line 5 if i ... Read More

Advertisements