![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
Found 27104 Articles for Server Side Programming
![codefoxx](https://www.tutorialspoint.com/assets/profiles/11160/profile/60_106352-1514269611.jpg)
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)
![codefoxx](https://www.tutorialspoint.com/assets/profiles/11160/profile/60_106352-1514269611.jpg)
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
![codefoxx](https://www.tutorialspoint.com/assets/profiles/11160/profile/60_106352-1514269611.jpg)
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
![codefoxx](https://www.tutorialspoint.com/assets/profiles/11160/profile/60_106352-1514269611.jpg)
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
![codefoxx](https://www.tutorialspoint.com/assets/profiles/11160/profile/60_106352-1514269611.jpg)
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
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
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
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
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'
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
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
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
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