Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, why dont I need to import RuntimeException to use it?

If I try to use IOException without the fully qualified name and without importing, I get a compilation error. But it doesn't happen when I do the same with, say, RuntimeException or Exception. Why is that?

Thanks

like image 881
dumptoday Avatar asked Mar 12 '12 00:03

dumptoday


People also ask

Is it mandatory to handle runtime exception in Java?

A user should not attempt to handle this kind of an exception because it will only patch the problem and not completely fix it.

Can we throw RuntimeException in Java?

yes you can. You can throw anything that extends Throwable - check the javadocs.

Why we should not catch runtime exception?

Usually, a RuntimeException indicates a programming error (in which case you can't "handle" it, because if you knew to expect it you'd have avoided the error).

What is a RuntimeException in Java?

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.


1 Answers

Classes from package java.lang. need not be imported. It's not just RuntimeException, but also stuff like Object and String.

like image 164
Jakub Zaverka Avatar answered Oct 19 '22 23:10

Jakub Zaverka