Welcome back.
In this view, we are going to check out exception handling, which means the handling of runtime problems
which occur in the program and would otherwise lead to program termination.
So this is the case for not only apps, but for all programs.
So what is an exception?
An exception is a runtime problem which occurs in the program and leads to a program termination, which
on the phone means that the app is not going to respond.
Now, that is a very, very bad behavior.
So you should at all costs, try to avoid exceptions to not be handled so much to just run.
So the examples would be running out of memory array, out of bound condition like divide by zero.
And to handle this type of problem during problem execution, the technique of exception handling is
used as an exception.
Handling is a technique which handles the runtime problems and maintains the flow of the program execution.
So when you use exception, handling the exceptions may still arise.
But they will not make your program crash and your program will still work, even though it might not
do the things as intended.
For example, if you try to access the Internet and you don't handle the exception that the Internet
connection doesn't work, then you would run into an error and you would get an exception because you
can't connect to the Internet and that could break your app if you don't handle this exception.
That's why it's super important to handle those kind of exceptions and yet still make your program work.
So even if the Internet doesn't work, then you will just displace something to the user.
Please turn on your Internet or get an Internet connection or something like that.
All right.
So there is the Thirroul class, which allows us to throw an exception.
So this throws an exception if we use to throw key word and then the name of the exception.
I call this exception my exception, because we can create our own types of exceptions, which will
then do whatever we want to do.
In those cases, there are four different keywords used, an exception handling, and these are try,
catch, finally and throw.
So these are the main keywords and we're going to check those keywords out.
So keywords use an exception handling.
Let's look at to try keywords.
The tribal contains a set of statements which might generate an exception.
It must be followed by either catch or finally or both.
All right.
So this is the code that can break, that can make and more create an error, create an exception such
as connecting to the Internet or running through an array.
So that can be a lot of issues that come up.
Then the catch block is used to catch the exception thrown from the tribe block.
So if there is an exception in the tribe, block by block should execute, otherwise it will not execute.
So if there's no problem in the tribe block.
So if the code works perfectly fine, for example, back to our Internet problem.
Let's say we have Internet and we try to exit the connection, then we will not have a problem.
The tribe will work.
The cashbook will not execute.
But if the Internet connection is not there, then of course the catch block should run.
Then we have finally the final block allows execution, whether exception is handled or not.
So it's used to execute important code statements like closing buffers, for example, closing a buffer
which takes care of the connection to the Internet or a buffer that takes care of storing data on the
phone.
So these are things that need to be closed after they have been opened, and that's usually done.
And finally, so after the tribe block has been executed or after the cashbook has executed, if there
was an error and then we have this through keyword, which allows us to throw an exception explicitly.
So we ourselves write this in order to throw an exception, in order to cause an error.
And this is especially useful when testing to see where an error occurs and we create our own exceptions
or throws so that we know, OK, this is something that we just wrote.
All right.
And then there you are sitting in front of your code and suddenly an exception arises.
Well, the thing is that usually happens when you have unchecked exceptions.
So an unchecked exception is the exception, which is thrown due to mistakes in our code.
So our code for some reason doesn't work.
And there are multiple different exceptions.
And the exceptions don't run during runtime or execute during runtime at runtime exceptions.
And they extend from this runtime exception class.
And the unchecked exception is checked at runtime, which means when our app is running and not during
compile time, meaning when we compile our app, for example, the run button.
Examples of unchecked exceptions are arithmetic exceptions thrown when we divide a number by zero,
for example, then the index out of bound exception is thrown when an array has been tried to access
with incorrect index values.
So let's say the array only has five values, but you're trying to access to sixth or seventh value.
Then you get this error.
You can very easily try that and force this kind of an error.
And you probably have seen this already.
And then the security exception thrown by the security manager to indicate that we, yeah.
Have a security violation.
So, for example, we're trying to access GPS, but we don't have the rights to access because the user
didn't give us the rights to use the had GPS functionality and then the very common null pointer exception
thrown when invoking a method or property on a null object.
So when we tried to access an object which is empty, which is now, then we get this now pointer exception.
And that is something that I have already talked about when we looked at Nullable.
Then check the exceptions.
Well, they are exceptions which are checked at compile time, so pretty much in our code.
We write this ourselves.
The exception type extends the Froebel class.
So there's a class called Froebel, which takes care of the exception.
Functionality and examples of checked exceptions would be the input output exception.
So eyehole exception or the simple exception.
And there are a bunch more.
Now let's look at the try catch block, which is used for exception, handling in the code.
The syntax of try with each block looks like this.
So we have distri then code that may throw the exception and then we have this catch, for example,
some X exception.
So this could be the only exception if it's an input output exception or it could be just a generic
exception.
So we would just use the generic exception class name here.
And then we have our code that handles the exception.
And of course, we need to close the bracket here.
All right.
Examples without exception, handling.
So in this case, we have an exception handling written ourselves.
So try and catch.
But the exception is never arising because the problem doesn't appear so here.
For example, we use value string get no we so we use this function, get no and we have a value of
10 there.
So the variable string is getting the value of 10 and making a string out of it.
So we're trying to interchurch parse it.
So we're trying to parse that value into a string.
So it's an integer, right.
10, but we're making a string out of it.
And if something goes wrong, for example, an arithmetic exception appears, then we want to output
zero.
All right.
So if we run print and string now, we get the output of 10.
All right.
Now let's look at the exception handling when it's actually running.
So when we actually do have an exception handling running.
And that is the case.
For example, if we try to have a parsing here with a value of ten point five.
So in this case, we're trying to pass a double as an integer and that doesn't work.
So we have to be careful with the type here, of course, and that creates an error.
So our try block does actually create an arithmetic exception.
So the output is zero.
So whatever we have in our catch block here.
All right, so that is something that we, of course, have to be careful with, then we can have multiple
catch blocks so we can use multiple catch blocks in our code.
And they are used when we can have different types of operations or, uh, different types of operation
errors in a blog which may cause different exceptions in the tribal.
So here an example.
We have this function where we try.
To get the value a out of an entry, which is five, so which has five elements in there, and now we're
seeing a at position five, which would indicate five, which means that its sixth position should be
ten divided by zero.
So there are a bunch of problems that we create here.
One of them being that 10 cannot be or anything cannot be divided by zero.
So we get an arithmetic exception here, trying to divide by zero, not good, doesn't work, creates
an error.
And then we're trying to exis an array at a position which doesn't exist.
So the array just has five values, but we're trying to exis its sixth value.
So we're creating this problem here.
So here this is where the problem arises.
All right.
So we have two errors that can appear here, and then we have the arithmetic exception, which takes
care of arithmetic's more than we'll just print arithmetic such and catch.
So, for example, trying to divide by zero.
Then we have an array index out of bounds exception which will be run because of this, a accessing
index five.
And then we have a generic exception, which is the apparent exception clause.
So it's just the name exception.
So you can see in catch we always have this E Colon and then the name of the exception that we want
to use.
And by the way, you can get information about the arrow.
So a little more information or pretty much a lot of information when you just print the stack trace
of it.
All right.
So we can see that the output here will be an arithmetic exception catch.
And then we have the code after the try catch so we can also have nested try catch blocks and that is
sometimes required.
But the requirement of no to catch blocks arises when a block of code generates an exception and within
a block, another code statement also generates an exception.
So that is the exception.
Inception, so to speak.
All right.
Syntax of nested try blocks.
Let's look at this.
So we have a tri code block and then we have another try code block and then we have the catch block
for the first try and then add some exception code that we have.
And then we have another exception that we catch.
So as you can see, the tree has to be followed by a catch.
But when you have a try inside of a try, that try has to be followed by a catch.
OK, so these two are connected with each other and this tri here is connected to that Ketso down there.
All right.
So this is an example for that.
Then we have the final block and you saw the key word finally before and the final block is such a block
which is always executed, whether exception is handled or not.
So it is used to execute important code statements.
For example, as I said earlier, code that should be run when all of the other coaches done, for example,
closing the connection again.
So we have this function main here where we try to divide ten by five and then we print the data.
So our catch that we have here is a no pointer exception.
So.
We have this exception that we pointed here, because data could be empty, for example, and then we
have this final blog here which always executes and then we have print line below code just to see what's
executed.
And if we run this code, we will see that to then finally block always executes and pillow code are
printed onto our console.
And that has to do with, first of all, ten divided by five is two.
So this print statement is printed.
Then here you see we print the E!
So the null pointer exception.
So if that would arise, we would print the whole exception.
And then we have this final block which is executed no matter if Trie is running without an error or
if it's run with with an error.
So the catch block will be run as well.
So either way, we run our finally so our print finally always executes is executed and of course,
our below code is also executed.
Then we have this through keyword and through keyword is used to throw an explicit exception and it's
used to throw a custom exception, for example, to throw an exception object we would use to throw
exceptions.
So the syntax of the throw keyword is throw some exception.
And let's have a look at it for this.
We have this from Maine where we use this validate function and we add the value of 15 in there or we
pass a value of 15 and we print some code after the validation check or pretty much after the validate
function.
So in the validate function, we check if the H is below 18 and if that's the case, then we throw an
arithmetic exception where we say under H and if the H is not under 18, then we say eligible for drive.
So this is not the case for the US, but it's the case for many other countries where you have to be
18 in order to drive.
So the output here is exceptional.
In four main Java dot lanctot arithmetic exception, colon under H.
So after the colon comes whatever we have entered in those brackets.
OK, so in our case under H.
So this is something that comes in handy when you actually want to make sure that you check your code
correctly and you just want to throw these kind of errors in order to break your code, in order to
understand where the problems arise.
So in this case, it wouldn't make sense because it's a dumb example, so to speak.
But in real life, it would make a lot more sense when you, for example, need to check your app.
For potential incorrect entries that users can make or for potential problems that arise when you get
data from the Web and stuff like that.
And you don't want to publish the app with a lot of throe statements or a lot of errors, OK, because
a throwaway statement actually makes your app crash.
All right.
Now let's get back to coding.
This was a super long example of the whole concept of trying to catch blogs, but now you know pretty
much everything about it and we can use it in our code.
And all of this might look a little intimidating, but it would really become your second nature once
you use it and you see what the advantages of all of those things are.
So I'd say, let's get back to go and see in the next video.