Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Runtime Exceptions "unchecked" in Java?

Why does it make sense to have Runtime Exceptions UnChecked (as opposed to if they were Checked)?

like image 714
Moeb Avatar asked Nov 01 '09 04:11

Moeb


People also ask

Why does Java have unchecked exceptions?

An unchecked exception (also known as an runtime exception) in Java is something that has gone wrong with the program and is unrecoverable. Just because this is not a compile time exception, meaning you do not need to handle it, that does not mean you don't need to be concerned about it.

Is runtime exception is a checked exception or unchecked exception?

Run-time exception is called unchecked exception since it's not checked during compile time.

How are runtime exceptions different from checked exceptions?

Main difference between RuntimeException and checked Exception is that It is mandatory to provide try-catch or try finally block to handle checked Exception and failure to do so will result in a compile-time error, while in the case of RuntimeException this is not mandatory.

Can runtime exception be caught in Java?

Runtime exceptions represent problems that are a direct result of a programming problem, and as such shouldn't be caught since it can't be reasonably expected to recover from them or handle them.


3 Answers

If you didn't you would have to have try/catch blocks every time you accessed an array element, did a division operation and many other common scenarios.

To put it another way, imagine this code:

Map map = ...
int i = ...
(int[])map.get("foo")[3] = 2334 / i;

would have to check for ClassCastException, ArrayIndexOutofBoundsException, ArithmeticException, UnsupportedOperationException and NullPointerException just off the top of my head.

With Java the issue isn't unchecked exceptions. Checked exceptions are a highly controversial subject. Some say this was largely an experiment with Java and in practice they don't work but you will find plenty of people who argue they are good.

No one is arguing unchecked exceptions are bad however.

like image 66
cletus Avatar answered Sep 21 '22 02:09

cletus


The idea of the two kinds of exceptions in Java (checked and unchecked) is that checked exceptions should be used for error conditions that can reasonably be expected to happen, and unchecked exceptions should be used for unexpected error conditions.

For example if a file isn't found, you get a FileNotFoundException, and it's reasonable to expect your program to be able to handle such a condition. Unchecked exceptions should be used only for problems that shouldn't happen, and that really mean that there is a bug in the program if such a problem happens. For example, a NullPointerException means that your program is trying to dereference a variable that is null and that's most likely a bug.

The Java compiler forces the programmer to handle checked exceptions. This makes the programming language safer - it means that the programmer is forced to think about error conditions, which should make the program more robust.

The compiler doesn't check unchecked exceptions, because unchecked exceptions aren't supposed to happen anyway and if they do, there's nothing that the program could reasonably do at runtime; the programmer must solve the bug.

There has been some criticism to this feature in Java, some people even call checked exceptions a failed experiment and some people propose to remove checked exceptions from Java.

like image 32
Jesper Avatar answered Sep 23 '22 02:09

Jesper


This simply means that the compiler will not force you to look for an exception, but you can still throw it at runtime. As one benefit, this allows you to throw new exceptions from your classes without requiring you to alter your interface, causing callers to change their code.

like image 41
jheddings Avatar answered Sep 19 '22 02:09

jheddings