Hello, in this video, I am going to show you Exception Handling through the Troy fro and hash blocks, the kind of almost like an if statement, they like, try to do something and if some error occurs, maybe you manually trigger an exception, feel free to check out the previous video on how to do that. Or maybe something has gone wrong, for example, you're trying to load the file in. And if you load a file that does not exist, or you have a incorrect path or something like that, you could handle it like this instead of the entire application mess, you know, instead of having like a custom exception handler for everything, what we can do get rid of this, we'll get rid of this. We'll still do something like this. But what we're going to do is imagine if this is some user input, you know, dollar ROI, and we can have a try block for the sake of choice some code.
If it works fantastic. If not, then we're going to catch an error and we are going to say Create the function, so function check number and taking one parameter number. And if the number is greater than 10 then we'll throw the exception towards a cut that, put that there. And we can return true, if you know if everything's okay. And then we could potentially do something with this, we're not going to but you can do for you one too. And in here, what we're going to say is checking on the check number.
And then we're going to pass in a value of 11, which is larger than the maximum of 10. I'm going to put an echo here, so he's gonna say everything Okay, for a new line turn. So if you see this, that means this exception wasn't thrown. But if you do throw the exception or if you don't get from Point A catch block, and the catch block, just take an exception call it E. And we just echo is going to echo message of exception is and then we'll go into append dollar a message. So if we run that message, a second number is too large, Aleksey, this isn't printed out, but I thought I wanted this to be dollarized. So we can easily just change up here.
And this should still you know, mess up. Okay, so for Chinese Now 10 even greater than 10. So we get everything is a Okay, so he goes through here does the check number functionality 10 is greater than 10. Therefore this exception in the front, it returns true, everything's okay. And only this is one if an exception is from. So this is great if you want to throw an exception manually based on some sort of data, for example, maybe the user has in put also, again, it's also great if you are doing something like opening a fall, you would enclose something that you know could potentially fail and go wrong in a try catch block.
And if it's all okay, this will never get executed. If it isn't, then you're handling in some form here. You might have some sort of backup, maybe you are trying to open a file. If that doesn't open, you just have some default hard coded code in there, maybe some data for a date, I mean, some data for table that you're going to display. And if you can't get the dynamic data from a database or from a fall, you might simply just put some dummy data in there. So yeah, that is it.
That is how you use Detroit throw and catch block for exception handling. What I want you to do as extra task is try and do in a more of a in a more natural way, but try and trigger it without manually triggering it. So do something like we try to load a file opening a file that doesn't actually exist, or you put the incorrect name, which technically doesn't exist then. And that's it. Any questions? Feel free to pop me a message and as usual, I look forward to seeing you in the next video.