Rajendra Dharmkar has Published 452 Articles

What is the best way to log a Python exception?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:03:39

315 Views

We import the logging module and then use the logging.exception method to create a log of the python exception.Exampleimport logging try: print 'toy' + 6 except Exception as e: logging.exception("This is an exception log")OutputWe get the following outputERROR:root:This is an exception log Traceback (most recent call last): File "C:/Users/TutorialsPoint1/PycharmProjects/TProg/Exception handling/loggingerror1.py", ... Read More

How to capture and print Python exception message?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:02:12

1K+ Views

Python exception messages can be captured and printed in different ways as shown in two code examples below. In the first one, we use the message attribute of the exception object.Exampletry: a = 7/0 print float(a) except BaseException as e: print e.messageOutputinteger division or modulo by zeroIn case of given ... Read More

How do you handle an exception thrown by an except clause in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:01:09

151 Views

We have a scenario in which code in except clause itself raises exception. In given code, we can handle the exception raised by except clause as follows.Exampleimport sys try: a = john except: try: 4/0 except: print sys.exc_info() OutputWe get the following output"C:/Users/TutorialsPoint1/~.py" (, ZeroDivisionError('integer division or modulo by ... Read More

How to catch ZeroDivisionError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

436 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 ... Read More

How to catch FloatingPointError Exception in Python?

Rajendra Dharmkar

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: ... Read More

How to catch StandardError Exception in Python?\\\

Rajendra Dharmkar

Rajendra Dharmkar

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

392 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 ... Read More

How to catch StopIteration Exception in Python?

Rajendra Dharmkar

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 ... Read More

How to catch SystemExit Exception in Python?

Rajendra Dharmkar

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 ... Read More

How to catch NotImplementedError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 10:36:48

1K+ Views

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 use Python object in C++?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 10-Feb-2020 10:49:28

685 Views

Here is an example in which a simple Python object is wrapped and embedded. We are using  .c for this, c++ has similar steps −class PyClass(object):     def __init__(self):         self.data = []     def add(self, val):         self.data.append(val)     def ... Read More

Advertisements