Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Exception/RunTimeException in java?

Tags:

java

exception

I have below classes.

public class ValidationException extends RuntimeException {   } 

and

public class ValidationException extends Exception {   } 

I am confused as to when the custom exception should extend RunTimeException and when it has to extend Exception. Could you please explain me is there any disadvantage of extending RunTimeException directly?

Thanks!

like image 438
user755806 Avatar asked Nov 08 '13 10:11

user755806


People also ask

What does extends exception mean in Java?

The class extends the Exception class that is defined in the Java core API (in the package is java. lang ). When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception .

What are the runtime exceptions in Java?

The most common Runtime Exceptions are NullPointerException, ArrayIndexOutOfBoundsException and the InvalidArgumentException. The Java Virtual Machine throws the first two Runtime Exceptions.

What are runtime exceptions in Java give example?

Runtime - runtime exceptions are internal to your application but are not typically recoverable. For example, an object that is expected to have a value but is actually null. In this case, a NullPointerException exception would be thrown.

Is it good practice to throw RuntimeException?

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.


1 Answers

RuntimeException are unchecked while Exception are checked (calling code must handle them).

The custom exception should extends RuntimeException if you want to make it unchecked else extend it with Exception.

With unchecked exceptions calling code method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

As the calling method may not handle `RuntimeException``, one needs to be careful while throwing RuntimeException.

Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).

Source/Further Reading: Unchecked Exceptions - The Controversy

like image 90
Ankur Shanbhag Avatar answered Oct 08 '22 05:10

Ankur Shanbhag