Found 27104 Articles for Server Side Programming

How to catch SyntaxError Exception in Python?

codefoxx
Updated on 12-Jun-2020 08:02:01

2K+ Views

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 handle the exception and find its typeExampletry: print eval('six times seven') except SyntaxError, err: print 'Syntax error %s (%s-%s): %s' % \ (err.filename, err.lineno, err.offset, err.text) print errOutputC:/Users/TutorialsPoint1/~.py Syntax error (1-9): six times seven invalid syntax (, line 1)

How to catch EOFError Exception in Python?

codefoxx
Updated on 12-Feb-2020 10:50:04

683 Views

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 and find its type.Example#eofError.py try: while True: data = raw_input('prompt:') print 'READ:', data except EOFError as e: print e Then if we run the script at the terminal $ echo hello | python eofError.pyOutputprompt:READ: hello prompt:EOF when reading a line

How to catch NameError Exception in Python?

codefoxx
Updated on 12-Feb-2020 10:48:51

496 Views

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.Exampleimport sys try: def foo(): print magnolia foo() except NameError as e: print e print sys.exc_type OutputC:/Users/TutorialsPoint1/~.py global name 'magnolia' is not defined

How to catch IndexError Exception in Python?

codefoxx
Updated on 12-Feb-2020 10:47:43

3K+ Views

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: 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

How to catch OverflowError Exception in Python?

codefoxx
Updated on 12-Feb-2020 11:00:30

2K+ Views

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.ExampleGiven code is rewritten to catch exception as followsi=1 try: f = 3.0**i for i in range(100): print i, f f = f ** 2 except OverflowError as err: print 'Overflowed after ', f, errOutputWe get following OverflowError as output as followsC:/Users/TutorialsPoint1/~scratch_1.py Floating point values: 0 3.0 1 9.0 2 81.0 3 6561.0 4 43046721.0 ... Read More

How to catch ArithmeticError Exception in Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:07:42

842 Views

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 followsExampleimport sys try: 7/0 except ArithmeticError as e: print e print sys.exc_type print 'This is an example of catching ArithmeticError'Outputinteger division or modulo by zero This is an example of catching ArithmeticError

How to catch IOError Exception in Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:08:32

7K+ Views

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 input/output exception, which is handled in the except block as shown given belowExampleimport sys def whatever(): try: f = open ( "foo.txt", 'r' ) except IOError, e: print e print sys.exc_type whatever() Output[Errno 2] No such file or directory: 'foo.txt'

Where can I find good reference document on python exceptions?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

56 Views

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

How to get Python exception text?

Rajendra Dharmkar
Updated on 26-Sep-2019 20:03:49

884 Views

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 that has occurred; value contains error message; traceback contains stack snapshot and many other information details about the error message.The sys.exc_info() function returns a tuple of these three attributes, and the raise statement has a three-argument form accepting these three parts.Getting exception type, file number and line number in sample ... Read More

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

Rajendra Dharmkar
Updated on 26-Sep-2019 20:04:17

140 Views

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 is the base class exception of which pretty much all other exceptions are subclasses. If e is an exception object, then e.args and e.message are its attributes.In current Python implementation, exceptions are composed of three parts: the type, the value, and the traceback. The sys module, describes the current exception ... Read More

Advertisements