- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rajendra Dharmkar has Published 452 Articles
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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
Rajendra Dharmkar
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