Codefoxx has Published 5 Articles

How to catch SyntaxError Exception in Python?

codefoxx

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

How to catch OverflowError Exception in Python?

codefoxx

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

How to catch EOFError Exception in Python?

codefoxx

codefoxx

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

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

How to catch NameError Exception in Python?

codefoxx

codefoxx

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

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

How to catch IndexError Exception in Python?

codefoxx

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

1
Advertisements