Hello, in this Java programming video, we are going to look at creating our own exception. So first thing you want to do is go to the package right click, go to new class, not really going to be covering classes in this video that's in a separate section. So feel free to check that out. And I was gonna call this more in here before automatically created a class which is cool. What we want to do and do extends exception to that means is a you know, a class that can handle an exception. I can use one exception, not on the string.
And on the string I'm going to put is call it str four cola. Now, you put the name of the class of mind exceptions. So this is the actual exception name and the parameter so I'm going to use the same. We're going to use I'll use this to London. Okay, yeah. Okay, let's do that.
Okay, so this will require a parameter to be used with the exception. So I'll just have a string in here. And what we're going to do is say str one equals str two. And what this means is public and it will return a string to string you don't need static because we're going to be creating For like an instance of this class, again, take it for granted for now, you want to know more information, check out the classes section, the each section in this series so to string. This is the exception error message that will get printed out, but we will return and we do exception I'm gonna just put plus STr one. And that is the reason why we do str one equals str two.
Because the you know, whatever is passed in here, so you know, why would data has you know, made the exception get called, is signed to str one and that's what can get printed. So in sonar systems dot java, what we are going to do is do a tried lots of try Catch, block, catch, block, and don't be like so. unreachable This is never from from the try catch up for everything, it just needs to come down. So I'm just gonna say, flow, user will throw it ourselves. That's how you do custom exceptions. So that way, if you know you have a bit of code, maybe get some input from the user.
And if it's a certain type of input, or like certain values, that's great continue. If not like using something like an if statement. if else here for if else, then you could throw your custom exception and handle it accordingly. So you're not just beholden to the exceptions there are built in so if I put here So, no exception type can be thrown exception time must be a subclass of throwable. Look here Okay, we need to import this import. Okay.
Let's just try one other way. Let me just simply just try getting this code put in here. If we just do a regular class or a public class, we can do it in the same file we'll actually cover importing modules because you know, at the moment it's just getting a bit too in depth going into other parts of Java. So we'll just put it in the one for now. Just to make things easier Okay, so was this era with my exception cannot be resolved to a type or kill don't really mean much to me. The question is why?
And then not only from this era and no Project Setup to be fixed. Okay, so now the way that errors are apparent, okay, so now we actually need to do a system dot out, dot print ln. And in here we'll print out error. I didn't see my exception was triggered, which is from here. So this is my custom error method. So the for the exception again, you'll probably want to do some if statement or somewhere where it's triggered specifically Quickly, you passing the message to the have to be a string, if you don't want to swing, you can have integer is dead was struggle probably just be easier, you can always cast a number variable to a string as well.
And that's it gets thrown into here. And this will just generally be used for your custom exception. You can have multiple classes for multiple custom exceptions. If you have any questions, feel free to reach out. As I said, we have a separate section, which covers you know, just object oriented programming in the form of classes. So if any of this just seems a little bit out there, so feel free to check that out.
And I look forward to seeing you in the next awesome Java video.