how to print error in try except python
try:
# some code
except Exception as e:
print("ERROR : "+str(e))
how to print error in try except python
try:
# some code
except Exception as e:
print("ERROR : "+str(e))
python exception list
AssertionError #Raised when assert statement fails.
AttributeError #Raised when attribute assignment or reference fails.
EOFError #Raised when the input() functions hits end-of-file condition.
FloatingPointError #Raised when a floating point operation fails.
GeneratorExit #Raise when a generator's close() method is called.
ImportError #Raised when the imported module is not found.
IndexError #Raised when index of a sequence is out of range.
KeyError #Raised when a key is not found in a dictionary.
KeyboardInterrupt #Raised when the user hits interrupt key (Ctrl+c or delete).
MemoryError #Raised when an operation runs out of memory.
NameError #Raised when a variable is not found in local or global scope.
NotImplementedError #Raised by abstract methods.
OSError #Raised when system operation causes system related error.
OverflowError #Raised when result of an arithmetic operation is too large to be represented.
ReferenceError #Raised when a weak reference proxy is used to access a garbage collected referent.
RuntimeError #Raised when an error does not fall under any other category.
StopIteration #Raised by next() function to indicate that there is no further item to be returned by iterator.
SyntaxError #Raised by parser when syntax error is encountered.
IndentationError #Raised when there is incorrect indentation.
TabError #Raised when indentation consists of inconsistent tabs and spaces.
SystemError #Raised when interpreter detects internal error.
SystemExit #Raised by sys.exit() function.
TypeError #Raised when a function or operation is applied to an object of incorrect type.
UnboundLocalError #Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
UnicodeError #Raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError #Raised when a Unicode-related error occurs during encoding.
UnicodeDecodeError #Raised when a Unicode-related error occurs during decoding.
UnicodeTranslateError #Raised when a Unicode-related error occurs during translating.
ValueError #Raised when a function gets argument of correct type but improper value.
ZeroDivisionError #Raised when second operand of division or modulo operation is zero.
print type of exception python
try:
someFunction()
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print (message)
except as Exception:
>>> def catch():
... try:
... asd()
... except Exception as e:
... print e.message, e.args
...
>>> catch()
global name 'asd' is not defined ("global name 'asd' is not defined",)
try except python
try:
print("I will try to print this line of code")
except ERROR_NAME:
print("I will print this line of code if error ERROR_NAME is encountered")
how to use except statement in python
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print("division by zero!")
... else:
... print("result is", result)
... finally:
... print("executing finally clause")
...
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us