Hello.
Welcome to the session on errors and exceptions.
There are two types of arrows that typically occur when writing programs.
Syntax errors and exceptions syntax errors occurred due to incorrect structure of the program.
This means that the programmer has made a mistake in the structure of a statement or an expression even
lousy.
An example in the Jupiter note book.
In this example here I have a print function that in an if statement and when we executed this line
Python interpreter has thrown a syntax error.
And if you notice in the output the line where the error was detected is displayed.
And also we have a little arrow here pointing at the earliest point in the line where the error was
detected.
And if you notice carefully the colon that should follow after the next statement is missing and apart
from the error messages and the line where the error was detected in the output the file name and a
line number are printed so we know where to look.
In case the input came from a script so we will put a call in after the if statement here and then execute
this line.
So there are no syntax errors in this line.
All right next even if a statement or an expression is syntactically correct it may cause a Nader when
an attempt is made to executed and errors that occur while executing a program are known as exceptions
and we will see examples of different types of exceptions built in exceptions raised while executing
our code.
Both types of these errors these syntax errors and exceptions hide the execution of the program as soon
as they're encountered.
So now let's go and see some examples of exceptions next.
In this example here I'm trying to divide the number two by zero and when we tried to execute this line
the Python interpreter has thrown this exception and in the last line of the error message indicate
what happened says Zito.
Division error and the rest of the line provide details based on the type of the exception and what
caused it.
So.
Division by zero is not allowed.
So let's change this denominator.
Say we will give 2 divided by 7 and then execute so here we have a floating point number.
Now let's look at another example.
Let us know divide the number 2 with debatable X and now the Python interpreter has raised an exception
and this exception is name error and the last line of the error message displays what kind of exception
is encountered.
So name error is the exception and in the details we see that the name X or the variable x is not defined.
So nowhere in our program we have defined the variable X and we have started using this variable in
our operations and it is not allowed let's look at another example.
We will not concatenate a string with a number and see if this is possible.
And then execute.
Now the Python interpreter has thrown an exception.
The exception is a tight budget.
That's because we are trying to concatenate a string but an integer which is not possible.
Now let let's define a list list one contains the numbers 1 2 and 3.
Now let's try accessing these elements in the list.
List of 0 gives me the first element.
Let's try to access element number four element add position 4.
So as you can see there are only three elements that we have defined in the list.
List 1.
But when we are trying to access the elements in the list we have given the index number as for and
there is no number.
With this index in the list and hence the interpreter has raised this exception Index a list index out
of range.
Next we will try to import a script using the import statement and then execute this line and the interpreter
has thrown this exception module not phone we have not created any module with this name test.
So these are all the different errors that have occurred while executing the program and these errors
are called exceptions.
And when this happens we say Python has raised an exception or an exception is thrown whenever such
errors or exceptions happen.
Python creates a special type of object which contains all the relevant information about the error
that has occurred.
For example it contains the line number at which the edit occurred.
The error messages and so on and by default the exceptions hard the execution of the program.
The last line of the error message indicates what happened and the rest of the line provides details
based on the type of exception and what caused it and the preceding part of the error message here shows
the context where the exception happened in the form of a stack trace back.
So when our program detects a fund that will keep it from completing as expected or will generate erroneous
results we should hold for the execution and report the error by throwing an exception and bringing
an exception to attention as call raising the exception and if the exception is left unattended the
default behavior is for the interpreter to print a full trace back an error message included in the
exception for some script.
This behavior is sufficient but it is nice to catch the exception and print a more user friendly version
of the error so we can decide how our programs respond to different types of errors.
We may want to prompt the user for more input or display extended a road or a warning message.