Check out a free preview of the full Java Fundamentals course
The "Rethrow 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 walks through how to rethrow an exception to avoid catching the exception in a try catch block.
Transcript from the "Rethrow Exceptions" Lesson
[00:00:00]
>> So, let's write a program that creates a new file, but it doesn't catch any exceptions. We don't feel like dealing with exceptions, all right? So, we can get past this if you're feeling a little lazy. All right, so back to Java. Let's make a new class. So we, let's just add it here.
[00:00:26]
What do y'all think? All right. Okay, we can make another one because it's still ThrowingExceptions, right? So, we'll make a new method and this one we'll call it public static void rethrowException. And I want to make the new file, so we'll say, File. Make sure you import it.
[00:01:04]
Nonexistent.txt. And I wanna say file that creates a newFile. So we already know that throws an exception, right? I don't want a try catch block here, okay? I mean, this is gonna happen. Sometimes you're like writing code and you're using a method from some other class. It throws an exception and you're like, I don't know how to handle that exception.
[00:01:40]
Just let it let it throw, right? And that's fine. If you wanna do that, you don't have a way to rebound from that. What are you going to do, like print the stack trace? That'll already happen anyway, right? So you might just want to say, okay, I don't want to handle it, instead I'm just gonna say that I throw this exception too.
[00:02:02]
And that's enough. I no longer need the try catch block. I basically said, hey, if you call me there's a chance that this exception might be thrown. I've pushed the burden of that to whoever is calling me. And they could push the burden if they want to, or they can handle it if they want to.
[00:02:24]
All right. Polymorphism also works in this case. So, if there's any unchecked exceptions or maybe there's 15 different exceptions that can be thrown and I don't wanna list 15 things here, I could just change that to exception. Put the super class. Basically I throw exceptions people. All right?
[00:02:49]
I'm gonna leave a couple of examples. So we'll say this and then let's make one more here, rethrowExceptions. Okay, and then I have this one exception but then I could do like Scanner or something, right? So, Scanner, how did that code look, fileReader, right? And then that throws a different kind of exception.
[00:03:24]
So I'm going to change that to Exception. That way that handles both.
>> Can you specify two in the header?
>> Yes. You could, you will just do a comma here.
>> Okay.
>> And then write the other one so, just like an input mismatch or something like that you could do that.
[00:03:50]
>> I have a question.
>> Yeah.
>> Earlier we were talking about interfaces if you are setting up a method. Writing in a class, but you implemented it from the interface, the interface also need to throw the exception?
>> Say one more time.
>> So we have interfaces and we have methods or like method signatures in those interfaces.
[00:04:11]
And you can use that to generate a method and a class to fulfill that [INAUDIBLE]. Does the signature or the method name, I guess suppose in the interface also need to throw the exception if you're declaring it in the method name?
>> Good question. Let's check that out.
[00:04:32]
Give me one second, to write this. All right, let's go to interfaces. So let's say I have a product interface. You are asking if this needs to like throws IOException or something like that. So you can definitely declare that there and then whoever inherits, they get that burden as well.
[00:05:00]
What I don't know is if I don't declare that, can I declare it in the overridden method? So let's look at Book and getName. Let's say throws. Yeah doesn't like it. Overwritten method does not throw an exception. So you would have to define it in the.
>> The interface.
[00:05:26]
>> The interface, yeah, good question.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops