Answers for "python create exception"

14

throwing an exception python

raise Exception("message")
Posted by: Guest on June-22-2020
11

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'
Posted by: Guest on April-28-2020
6

python error handling

try:
	#insert code here
except:
	#insert code that will run if the above code runs into an error.
except ValueError:
	#insert code that will run if the above code runs into a specific error.
	#(For example, a ValueError)
Posted by: Guest on September-01-2020
0

how to create my own exception in python

class MyException(Exception):
    pass

raise MyException("My hovercraft is full of eels")

"""With modern Python Exceptions, 
you don't need to abuse .message, or override .__str__() or .__repr__() 
or any of it. If all you want is an informative message when your 
exception is raised, do this
Posted by: Guest on August-31-2021

Code answers related to "python create exception"

Python Answers by Framework

Browse Popular Code Answers by Language