In Python, you can throw exceptions and catch them using try-except blocks.
To throw an exception, you can use the "raise" keyword followed by an exception type and an error message:
main.py34 chars2 lines
To catch an exception, you can use a try-except block. Here's an example:
main.py123 chars5 lines
In this example, if the user enters a non-integer input, the ValueError exception will be thrown and caught by the except block, which will print an error message.
You can also define your own custom exception types by creating a new class that inherits from the built-in "Exception" class:
main.py77 chars3 lines
And then you can throw and catch instances of your custom exception type:
main.py142 chars6 lines
gistlibby LogSnag