‫Welcome back.
‫In this video, you will learn how to catch errors and to do something when an error occurs and not
‫make your program crash or anything like that.
‫So a common usage of catch and finally together is to obtain and use resources in a tri block, deal
‫with exceptional circumstances and a catch block and release the resources in the final block.
‫And what that means is what we're going to cover in this video.
‫So in order to run into an error, let's do the following.
‫We use the console right line.
‫So enter CW double tap and here I say please enter a number and we're just prompting our user to enter
‫a number and then we want to store that in user input, which is just going to be a string variable
‫and it's going to read whatever the user has to enter or has to say, and it should be a number in the
‫end, right?
‫That's the goal that we have.
‫But if we do the following and user input as int and we use int, dot pass.
‫So you have seen pass already and we try to parse the user input that was given here by the user.
‫We try to make an integer out of it and now if you run that, please enter a number and during three
‫and our program is fine actually let's add a console dot read key here.
‫So we see even better that the program is still fine.
‫So please enter a number.
‫I'm just going to enter five and our program is still running and it didn't crash.
‫Everything is fine, so now let's run it again.
‫But instead of entering a number, let's add a letter or write some text.
‫That is not.
‫Passable into an integer what we're doing here in line 16 in my case.
‫So I'm going to enter and my program crashes and it says exception on handled system format exception
‫input string was not in a correct format.
‫All right.
‫So the string that was given by the user was not in the right format to parse it into an integer.
‫And that's what a format exception is all about.
‫Now we can handle that by creating a try and catch block.
‫So let's say we use try and here you can see there is an option to use.
‫Try and catch.
‫C or I can simply use the try double tap and it's going to create a try catch exception throw for me.
‫So now what I want to do is I want to try that code and if it didn't work then I don't want my program
‫to crash.
‫And that's what this whole try thing is about.
‫So whatever error, code error in the sense of during runtime error code is in here will be tried to
‫execute.
‫And if it didn't work, then the catch.
‫BLOCK will be executed.
‫So whatever is in this body or within the curly brackets here is going to be executed.
‫So now we only have the throw here.
‫So now let's get rid of throw because it's still throwing.
‫Yara Now let's write something to the console if an error occurs.
‫So we say something like format exception.
‫Please enter the correct type next time.
‫And that's something that we know what the type is.
‫A general user would not know that.
‫So here you would write something like Please don't enter letters, but only numbers or something like
‫that.
‫So let's have a look here again.
‫Let's run an error into an error again, and then we just get this information format an exception.
‫Please enter the correct type next time.
‫And our program didn't crash, so we didn't run into this exception in the sense of our program or our
‫ID telling us, hey, there is something wrong and you should well handle your code, right?
‫So that's generally how the structure is.
‫You have a try, so you try some code in here which could run into errors, which can have an exception,
‫and then you have a catch block which is executed whenever destroy didn't work out.
‫So whenever there was an error, this catch block is going to be executed.
‫What we have here is a catch with an exception.
‫An exception is a very generic error.
‫So it could be any type of exception, but if we actually run into that error, so let's get rid of
‫this code, we will see what type of exception we have created.
‫So I'm just going to enter something like whatever a string I'm entering.
‫And actually nothing happens because we don't throw.
‫So let's stop that.
‫Let's throw as we saw earlier.
‫Just throw the error and we run it again.
‫I'm just entering something here and it says system forward exception.
‫So we have a little more precise information about the actual exception.
‫So now we know, okay, it is a format exception, so we should use format exception here instead of
‫exception.
‫So a lot of exceptions here.
‫But what we use here is a format exception.
‫As you can see here, there is a class called system, that format exception and the exception that
‫is thrown when the format of an argument is invalid or when a composite format string is not well formed.
‫That's exactly what we have here.
‫So let's get rid of this throw and.
‫Be fine with that.
‫So now let's run it to a different era.
‫Now let's create a very long number.
‫Something that cannot fit into a string.
‫Because, as you might guess, or as you might remember, a string only has a specific amount of space.
‫So let's say I'm not sure how many numbers does this, but let's say only this amount of numbers can
‫be stored within an integer.
‫So all of this here is not going to work because it's an overflow.
‫So it's too much information for a string to handle.
‫So if I enter this now.
‫I get an input system overflow exception.
‫So here we are.
‫System overflow exception.
‫Well, you was either too large or too small for an RN 32.
‫All right.
‫That's what I said.
‫And now we need to catch that exception as well.
‫So there are two ways of how we could handle it.
‫So either we use a different exception here and we just use exception.
‫So we're just handling all other exceptions that there are generically and we simply say console dot,
‫red line,
‫general exception, something went wrong, for example.
‫So let's create a very long number actually once again like this and let's enter that.
‫And we see we have a general exception, even though that's a overflow exception.
‫So you can handle it that way.
‫So if you don't know all the different kinds of exceptions that you have there or you don't want to
‫catch them all individually, you could do this way or you could be that precise and simply enter the
‫right one overflow exception.
‫So now it's going to work and you see a system overflow exception.
‫Let's run that again and let's please enter a number and let's create a very long number here.
‫And we still get the general exception because we didn't enter a different text overflow exception.
‫The number was too long or too short for an in 32.
‫So let's have a look again.
‫And overflow exception number was too long or too short for in 32.
‫All right.
‫So this is how we can generally catch exceptions.
‫And if there are any other exceptions that can run, as you can see, there is an argument null exception.
‫So let's have a look at what that means.
‫If we can even get that and we see.
‫An exception.
‫Please enter the correct type of type next time.
‫So even if it's empty, in our case, we don't get this null exception because our input, even if we
‫don't enter anything, still has an empty string which is not null.
‫So we don't run in this according to this actual exception.
‫So as you see here, whenever you try to execute some code, Intel is even giving you some information
‫here.
‫So that's really useful.
‫You can see there is the argument null exception, the format exception and the overflow exception.
‫So we could even add another exception here and call that one argument is null exception or argument
‫null exception.
‫And we say something like argument null exception.
‫The value was empty now.
‫So in order to even run into this era, we would have to use nulls, which we haven't covered yet,
‫but we will do so later on.
‫And if we have them, you can try to do that.
‫So if you have seen the notable video already, you could try to run into that error.
‫All right.
‫So now you have seen how to use try catch.
‫And now there is one more thing that I would like to show you in this video, which is finally and finally
‫is done like that.
‫So you used the final keyword and then you use curly brackets again and within that code block here,
‫so within that body, you enter the code that should be executed whenever.
‫Try and catch our done so.
‫It doesn't matter if the error was run or if the error didn't work.
‫There was no error.
‫Finally, we'll still be called.
‫So this is called anyways.
‫So that means as well.
‫We will see in a second.
‫So let's run into an arrow and we see we have a four minute exception and then we see this is called
‫anyways and that's our final block.
‫And the idea behind this finally block is to, for example, if you tried to start an Internet connection
‫or if you try to write into a file, there eventually can happen in error.
‫So let's say the Internet connection breaks or you don't have access to a file that you want to write
‫into or some other exceptions that can come up and you want to close the connection or the file either
‫way.
‫So you have open to file and now you try to write into that file and after you have written into that
‫file within the try block and maybe it worked, maybe it didn't work, but what you definitely want
‫to do is to close the file again, or you try to download something from the internet and if it worked
‫or it didn't work, doesn't matter.
‫You need to close the connection to the Internet afterwards, and that's something that you would do
‫in the file.
‫BLOCK And that's very handy and quite important when working with input output types or yeah, generally
‫with connections to Internet, Bluetooth or whatever.
‫So as you see, there is a lot to know about try and catch, but the most important things you have
‫seen already.
‫So now you know how to use try you know how to use, catch how to find out exceptions.
‫In some cases it's not that obvious.
‫So in some cases you won't check which kind of exceptions that can be run.
‫But if you run into an exception in your code, then you know which kind of exception that is because
‫well, the idea is so Microsoft Visual Studio will tell you which exception it was and then you definitely
‫should try to catch that exception or just use a general exception type okeydokey.
‫So now is a little challenge for you because there always has to be a little challenge, otherwise you
‫might get a little, well, bored or you might get a little lazy.
‫It's really important that you try to type the code as well and check it out for yourself.
‫So the little challenge is please go ahead and divide by zero.
‫So that's simple.
‫Just try to divide by zero and use two variables for that.
‫All right, let's go ahead and try just that.
‫So I'm going to try to divide by zero.
‫I'm going to use two integers and none.
‫One is going to be five and n none two is going to be zero.
‫And now I'm going to use a resultant result is going to be entered here.
‫Result is equal to num one divided by num two.
‫So now let's try that without try and catch actually before we use try and catch.
‫So let's run it.
‫Please enter a number and then divide by zero exception, and I hope you handle that.
‫So now let's add a try and catch.
‫So try or actually just make it easier.
‫Let's enter.
‫Try here and use the block double tab.
‫And now within the try, I want to run that.
‫And if it didn't work, I simply want to write something like C double.
‫You can't divide by zero.
‫And now this.
‫The exception that we just had was so we'll look.
‫Divide by zero attempt to divide by zero, and it's called divide by zero exception.
‫So if you want to find that, you can find that down here.
‫So I'm just going to use exactly that.
‫I'm going to replace exception with divide by zero exception.
‫So now let's run that code again.
‫Please enter number that was from before and then it says can divide by zero.
‫Right.
‫So I hope you try that and I hope you achieve to do just that.
‫And you now can see how you can create your own try and catch this.
‫And in the next video we're going to go ahead and use some more fancy stuff.
‫So see you there.