Hey that.
Well everything is great but there is one point that we need to fix.
Well let's try one more time and add to this setting the fault interval and set for example the value
here is not numeric but some letters.
For example three letter C or S or include cookie and the application crashed let's look at the look
at
and here a look at we can read that number format exception happened and numeric form of exception for
S S S string.
This happened in main activity in line one hundred seventy five that is here we cannot recognize the
string as an integer so we need to somehow protect ourselves from these and we can do this using the
try catch construction.
What is the these construction.
Let's write it down here.
It is written as false.
All right.
Try then.
Catch is written here.
What is the meaning of these construction.
This construction of the Java language and it allows you to run some code for example.
This code with an error
if we put it here and there.
What happens that the code is executed.
And if in this tri code some kind of exception accuse then application does not crash or write it then
control passes to the Catcher Block and the code that is placed here is executed.
That is so we can output here toast.
For example
I write that some error happens
and here we must specify the type of exception.
There is a general type of exception code exception clause we can name the object as well like no matter
if we specify the general class exception and then no matter what the exception occurs in this code
which just in the try block in any case these strings will be displayed.
And then the program will run normally after this line.
We can specify one more branch of exception that is gadget blogs may be several Let's copy and paste
another catch a book.
Second for example we can specify here our particular type of exception number for exception.
For example call it an E F but this is Morse special case of exception which these class format exception
number format exception inherited from exception.
That exception is the general class number format exception is a specific exception that occurs when
the number format is incorrect as in our case we cannot recognize the string as a number for example.
But as you can see Andrew's student emphasized this and sees no formal exception has already been called
a court because it is caught here both in the general class and their sense is that the common clause
general clause can catch an exception.
No form of exception and class cast exception that we recently received and there are all kinds of exceptions
that are inherited from it.
And therefore after this it makes no sense to already poured this exception because the console will
not reach here will read it but we can do it differently.
We can here specify no format exception and here exception.
And let's change the names of course
here.
Just e exception.
And now everything is flood.
If an exception occurs in this court control is transferred first to the branch that catches no format
exception.
If this is the exception then this court is executed and we can write no format exception
happens
and if any other exception is thrown but not a number format exception then these branch will not be
executed.
And these branch will be executed with a common exception.
Let's try to run so that you understand now we have in the files shared preferences the three that we
proved that three letters US s and therefore when we lodged application this exception will trigger
in the method since we will call set interval form shared preferences in don't create minutes in the
interval from their shared preferences file is set which now contains the three letters s Let's restart
and see what happens.
And as you see a number for me the exception was throw to first let's do length long
and here too long.
So that the totals will show longer and restored
no format exception happens because we have exactly this exception which is given to us here in lockup
since we now have three letters s and shared preferences and we cannot recognize them how the integer.
This is three letters.
Therefore this exception appears if it speaks it with a valid value
for example 15
and words restored.
Now let's go out and come in and as you can see everything is going fine.
Now no doors appear in this court now.
They are no exception.
We can hear word in this court specifically the next exception.
As you know it is impossible to divide by zero.
But let's take and declare here the variable x and assign the value eight divided by zero.
For example the compiler does not swear as you see.
There is no right underline that is a compilation error is not issued.
This error is not recognizable.
Such errors can often a cure but not in such an explicit form of course.
But if you make some calculations and you do not expect that the result will be zero and then divide
somewhere by this result then these division by zero make you Let's run and see what happens
some error happens.
These branch work as I told you since this exception is no longer a number for math exception are these.
Let's take a look.
Huh.
It's already and look.
God is not defined.
But if we remove it from that tribe look and put it both and restart
then we will have a crash up and in the walk Earth we have a message.
I may take exception.
Divide by zero for these.
Use the try catch constructions so that these crashes do not exist exist.
You and I can look at look at as developers and the user will not understand anything why the application
was correct.
Therefore the try catch block is used and if some error accused you can show the user what happened
by writing a message to the user in the catch book.
There are a very large number of different exceptions.
We will encounter some more than once and you can read about the exception.
There is a lot of information about this on the internet but I think the point is clear.
Let's go back to our case.
It seems like everything is fine.
Let's restart
and again.
Oh it's input some kind of incorrect text
for example number one and some kind of lesser number format exception.
It seems not bad but their sense is that the value is already placed in the shared preference as far
as you can see it is displayed extracted from the file here.
In our summary of settings but we can prevent this.
We can't do this with their preference change.
Listen.