Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to capture and print Python exception message?

I have code that can raise an exception , How do I catch the exception message?
Rajendra Dharmkar
Answered on 6th Dec, 2017

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.try: 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?

I have code where an except clause throws an exception. , How to handle this exception?
Rajendra Dharmkar
Answered on 6th Dec, 2017

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.import sys try: a = john except: try: 4/0 except: print sys.exc_info() We get the following output"C:/Users/TutorialsPoint1/~.py" (<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo ... Read More

How will you explain that an exception is an object in Python?

Here is code in which an exception is raised. , Is ‘err’ an exception object and what does sys.exc_info() do?
Rajendra Dharmkar
Answered on 6th Dec, 2017

Yes in given code ‘err’ is an exception object.In python everything is an object. And every object has attributes and methods. So exceptions much like lists, functions, tuples etc are also objects. So exceptions too have attributes like other objects. These attributes can be set and accessed as follows. There ... Read More

How to get Python exception text?

In python code, how do I access/get the python exception text or message or information like the filename and line number where the exception occurred?
Rajendra Dharmkar
Answered on 6th Dec, 2017

If a python code throws an exception, we can catch it and print the type, the error message, traceback and get information like file name and line number in python script where the exception occurred.We can find the type, value, traceback parameters of the errorType gives the type of exception ... Read More

Where can I find good reference document on python exceptions?

I am interested in mastering python exception handling. Can somebody suggest where I can find a good reference document on python exceptions?
Rajendra Dharmkar
Answered on 6th Dec, 2017

The following link is a  good reference document on python exceptionshttps://docs.python.org/2/library/exceptions.html

How to catch IOError Exception in Python?

If I run the following code, I am getting an error. , How to catch this error and know its type?
Rajendra Dharmkar
Answered on 6th Dec, 2017

IOError ExceptionIt is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.If the given code is written in a try block, it raises an ... Read More

How to catch ArithmeticError Exception in Python?

If I run the following line of code, I am getting an error. , How to catch this error and know its type?
Rajendra Dharmkar
Answered on 6th Dec, 2017

ArithmeticError Exception is the base class for all errors that occur for numeric calculations. It is the base class for those built-in exceptions like: OverflowError, ZeroDivisionError, FloatingPointErrorWe can catch the exception in given code as followsimport sys try: 7/0 except ArithmeticError as e: print e print sys.exc_type print 'This is ... Read More

How to catch SyntaxError Exception in Python?

When I run the following line of code, I get error , How do I handle this exception and find its type?
codefoxx
Answered on 6th Dec, 2017

A SyntaxError occurs any time the parser finds source code it does not understand. This can be while importing a module, invoking exec, or calling eval(). Attributes of the exception can be used to find exactly what part of the input text caused the exception.We rewrite the given code to ... Read More

How to catch EOFError Exception in Python?

If I run this code at the terminal as follows , I get an error How do I catch this exception and know its type?
codefoxx
Answered on 6th Dec, 2017

An EOFError is raised when a built-in function like input() or raw_input() do not read any data before encountering the end of their input stream. The file methods like read() return an empty string at the end of the file.The given code is rewritten as follows to catch the EOFError ... Read More

How to catch NameError Exception in Python?

If I run the following code, I get an error , How to catch this exception and find its type?
codefoxx
Answered on 6th Dec, 2017

NameErrors are raised when your code refers to a name that does not exist in the current scope. For example, an unqualified variable name.The given code is rewritten as follows to catch the exception and find its type.import sys try: def foo(): print magnolia foo() except NameError as e: print ... Read More

How to catch IndexError Exception in Python?

When I am running the following code, I am getting error , How do I catch this exception and know its type?
codefoxx
Answered on 6th Dec, 2017

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 typeimport sys try: my_list = [3, 7, 9, 4, 6] print my_list[6] except IndexError as e: print e print sys.exc_typeOUTPUTC:/Users/TutorialsPoint1~.py list index out of range ... Read More

How to catch OverflowError Exception in Python?

If I run the following code, I get an error , How do I catch this exception and know its type?
codefoxx
Answered on 6th Dec, 2017

When an arithmetic operation exceeds the limits of the variable type, an OverflowError is raised. Long integers allocate more space as values grow, so they end up raising MemoryError. Floating point exception handling is not standardized, however. Regular integers are converted to long values as needed.Given code is rewritten to ... Read More

How to catch ValueError using Exception in Python?

If I try to run the following line of code, I get an error , How do I handle this error and find its type?
Manogna
Answered on 6th Dec, 2017

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.import sys try: n = int('magnolia') except Exception as e: print e print sys.exc_typeOUTPUTinvalid literal for int() ... Read More

How to catch LookupError Exception in Python?

When I am running the following code, I am getting error , How do I catch this exception and know its type?
Manogna
Answered on 6th Dec, 2017

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

How to catch EnvironmentError Exception in Python?

When the following code is run, I get an error. , How do I handle this exception and find its type?
Manogna
Answered on 6th Dec, 2017

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

Advertisements
Loading...
Unanswered Questions View All

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.