I've already read everything I could about this and I still don't understand how to use checked and unchecked exceptions. I think I can't still grasp the concept. I've read around StackOverflow that it's better to use unchecked rather than checked exceptions, but Eclipse forces me to use checked exceptions, as in FileNotFoundException
(AFAIK, if Eclipse forces me to insert a try/catch block, it's a checked exception). I'm wondering, is there any way to translate checked into unchecked? What the hell is handling per se? I do not understand what it is to handle an exception.
I have this example here, I would really like to know how to handle (?) this. This one is a checked exception, right?
public void readFile() {
File foo = new File("./foo.bar");
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(foo));
} catch (FileNotFoundException e) {
// What should I do here?
}
getDataFromFile(bufferedReader);
}
I've seen various things people do here. Some print a stack trace, that's what I usually do, and I don't see the problem with it. It gives me the information I need to debug. Some people ignore them, I think that shouldn't be done (I saw the JNode OS booter ignoring an exception). Some people simply add the throws
declaration in the signature. Some throw more exceptions inside that thing! (I think maybe this is what using unchecked instead of checked means?)
Furthermore, if you add the throws
declaration, you will be forced to put a try/catch block further up, and that's inconvenient if you have a very big application. I'm sorry, but I am simply clueless. Completely. I'm trying to learn good and elegant design and this is torturing me.
Unchecked Exception
is an exception due to something that shouldn't happen in the first place, and hence cannot be anticipated. It handles a case that would arise only if your program had a bug.
It is a subclass of RuntimeException
(which is a subclass of Exception
), and are usually implemented using IllegalArgumentException
, NullPointerException
, or IllegalStateException
Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time.
Checked Exception
is something that could happen, due to unforseen circumstances with the rest of the system. It is outside your direct control, but isn't necessarily a bug, but a case that could be encountered.
It is a subclass of Exception
.
From: Exceptions
The file not being present is a case you need to handle as it could happen, even in production code, if certain conditions prevail (disk being full). That makes it a checked exception.
Exceptions are intended towards providing a better user-experience. So you should report the error to the user, and gracefully terminate if you cannot continue.
If it is indeed a case you have anticipated, then you should probably inform the user of the issue, and terminate gracefully if necessary, or move on to do the next thing.
If it is an unchecked exception, the best you could do is to tell the user than an unexpected error occurred, as it isn't something that should happen in the first place, and get the stacktrace reported back to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With