Java Fundamentals

Throwing Exceptions

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Throwing Exceptions" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates flagging errors within code by manually throwing exceptions. The thrown error messages can be customized or left as default.

Preview
Close

Transcript from the "Throwing Exceptions" Lesson

[00:00:00]
>> So sometimes you're gonna find yourself needing to flag errors within the code that you write, right? And you can do this by throwing exceptions yourself. So I'm gonna teach you how to do that. We're gonna write a method that calculates an employee's pay, but it does not allow overtime.

[00:00:17]
So remember, we wrote something like this very early on and we did a while loop right to input validate the input well now we're a little bit more mature we can throw an exception if we want to. All right, so let's go ahead and make a new class.

[00:00:40]
And we'll call this one, ThrowingExceptions. All right, inside of this class. Let's do a public static void, not void, double. And we'll call this method calculatePay. And this method will accept hours, and it'll also accept a pay rate. Okay. And we want to say that this method also throws an exception.

[00:01:35]
So to do that, after the parameter list, you would write the word throws and then what exception do you want to throw? So for this one we want to throw negative input exception, which does not exist. So I'll also teach you how to create an exception. So Java has a bunch of exceptions that you can pull from, if none of them meet your needs, you can write your own.

[00:02:07]
>> You just like extend the overall exception class.
>> Let's see. All right, so inside of here, let's go ahead and should we create the exception first. All right, I'm gonna put this here so that everybody can see it better. All right, so we got this negative input exception.

[00:02:33]
It's basically just a class, so let's go back to our exceptions. We're gonna make a new class, give it a name. And then yes, you are just going to inherit from exception because you get a lot of good free stuff, right out the box, right? And all I would really need, like that get message and all of that stuff will just automatically be here for you so you don't have to like re-implement all of that stuff, which is great.

[00:03:12]
The only thing you really need to add here is a couple of constructors that provides like a cause or the message right so we're gonna provide a default constructor. Just so people have that, this is a be a good example to show you something else and then let's create another one that allows people to give us a message.

[00:03:47]
>> So it's this overloading the first one.
>> Well I guess technically but they aren't methods constructors aren't methods but technically, yes. All right, so what I wanna do with this one I wanna call my super, which is who's my super?
>> Exception-.
>> Exception is my super, and I wanna give it the message that was given to me.

[00:04:19]
All right, so that'll call the constructor from exception that accepts a string. And then in my default one, we don't gotta get too jazzy. I'm gonna call this And I'm gonna give it my own message. So if they called me without a message, I better make a message up.

[00:04:38]
So I'm gonna say input must be greater than or equal to 0. What is this doing? Anyone?
>> Calling the default constructor of exception?
>> No.
>> The specific exception object for the negative input exception?
>> Say it one more time.
>> Well, it's like we were messing with those gutters and centers.

[00:05:14]
We had this dot where it's referring to the specific object of whatever you're talking about in this case, a negative input exception.
>> All right. Say it plain.
>> It is. I don't know how to say it.
>> Tell me when I call this, where am I going?

[00:05:30]
Go on.
>> Line nine.
>> I am going to line nine, okay, very good. So, when I caled this with those, I could say dot, right, remember we always talked about this, this refers to this class. So, when i call this dot I'm saying okay this dot and then any of its fields or methods or whatever when I say this with parentheses I'm calling the constructor.

[00:05:57]
And specifically whatever constructor matches the argument that I'm sending so I gave it a string it must be calling this constructor that takes a string. So instead of me like not providing this one right I could have just provided the other one I think that will work just fine let me see, yeah.

[00:06:21]
>> But then anyone who no It doesn't like because I didn't never mind. So here, I think I just have an exception because I didn't throw it. Yeah, I don't have a return statement with something like that. So I don't need this here, but it's a great way for me to provide a message by default, Right?

[00:06:43]
And I could have just called super with this. That's another way I could have done this. I could have just said, super, right? But, that's kinda what I'm doing here/. So I want to just say call my thing that calls the thing. Just in case this ever gets updated to do more than the super I got that cover.

[00:07:11]
Now so I just wanted to flex and show you a couple of different ways to do stuff. All right, so back to, we have our exception. Well look at that little icon, that means exception class. Wow, that's fancy. Y'all see? Okay, all right IntelliJ. All right so inside of our calculatePay let's go ahead and we're going to say all right if hours is greater than 40 right that means the overtime then we want to throw an exception.

[00:07:56]
How do we throw exceptions? Throw? We use the word throw. We say new and we call whatever exception constructor we want, negative input exception. So here I have two options, I could give it a specific message or I can just use the default constructor and that already provided a message, right?

[00:08:20]
>> What was the message you used on the initial exception that you wrote again?
>> I see Input must be greater than.
>> So then you would want a new message because that's not what's going on there.
>> Here, that's right. So I actually want to throw a different kind of exception.

[00:08:40]
So I want to show you all how you can do multiple, like you can throw multiple exceptions from like the same class
>> Over time violation exception maybe.
>> I'm gonna use one that's already in Java so we don't have to create another one but I can say like, illegal argument exception or something like that.

[00:09:01]
There's so many exceptions you can choose from you.
>> So I could do something like that and then I could change the messaging. So again, that's why you have one that allows people to put the message. So I could put something that's really specific to my case, such as like ours must be less than or equal to 40 less than or equal to 40.

[00:09:29]
Okay, so that'll call one of Java's built-in exceptions. And the illegal argument exception is a runtime exception, which means that it is unchecked exception. So even though I am throwing this method from throwing this exception from this method, I do not have to declare that in my method header.

[00:09:55]
All right, so okay, if the hours is greater than 40, we wanna throw that exception. Let's say else if the hours is less than 0 or the pay rate It's less than zero. So we got these negative numbers, right? So that's when we can call that negative input exception.

[00:10:21]
So let's say throw new negative input exception
>> And that one you could leave to the default messaging.
>> Yep. So finally, if we make it through all of this, if we throw the exception, this method is over, right? So if we make it past all of this, then we're good to go in, the home stretch, and we can go ahead and, add our return statement.

[00:10:45]
So we want hours, times, the pay rate So this was a good example because we created our own exception, we threw our own exception, so that's a nice custom one. We also threw an exception that already exists, in Java we throw an unchecked exception as well as a checked exception.

[00:11:14]
So we see that the checked exception is the only thing that we have to declare in the header as throws and we don't have to do the one here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now