3. INTRODUCTION
Until now error messages haven’t been more than 2 type.
There are (at least) two distinguishable kinds of
errors: syntax errors and exceptions
Source : https://docs.python.org/3/tutorial/errors.html#exceptions
4. SYNTAX ERROR
The parser repeats the offending line and
displays a little arrow pointing at the earliest
point in the line where the error was detected.
The error is caused by the token preceding the
arrow. In the example the error is detected at the
function print() since a colon (:) is missing
^ : indicate the part of error
5. EXCEPTION
Even if a statement or expression is syntactically
correct, it may cause an error when an attempt is
made to execute it. Errors detected during
execution are called exceptions and are not
unconditionally fatal
6. EXCEPTION
The last line of the error message indicates what
happened, exception come in different types, and
the type is printed as part of the message. For
example ZeroDivisionError, NameError, and
TypeError. The string printed as the exception
type is the name of the built-in exception that
occurred.
11. EXCEPTION HANDLING
It is possible to write programs that handle selected exceptions, like example in the
above which asks the user for input until a valid integer has been entered, but allows
the user to interrupt the program using “ctrl + c”
12. EXCEPTION HANDLING
The try statement works as follows:
1. First, the try clause (the statement between try and except keywords) is executed
2. If no exception occurs, the except clause is skipped and execution of the try statement
3. If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if it’s type
matches the exception named after the except keyword, the except clause is executed, then execution
continues after the try/except block
4. If an exception occurs which does not match the exception named in the except clause, it is passed on to
outer try statement; if no handler is found, it is an unhandled exception and execution stops
13. RAISING EXCEPTION
The raise statement allows you to force
an error to occur. We can define both
the type of error and the text that prints to
the user. That the argument to raise must
either be an exception instance or a
subclass deriving from an exception
14. RAISING EXCEPTION
In the code block in the left, TypeError is
the specified error. It has been set to
occur anytime the variable x is not a
string. The text inside the parentheses
represents your chosen text to print to
the user