Why we have Runtime Exceptioons in Java

Oracle  defines Exception as

 "An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions."

We have three categories of exception as:-

Checked Exceptions , which are subject to catch . All exceptions are checked exceptions, except for those indicated by ErrorRuntimeException, and their subclasses

Runtime Exceptions are internal to the application and system does not expect to recover from. NullPointerException is type of RunTime Exception

Error are exceptions which are external to applications like OutOfMemory Exception, StackOverFlow Exception.
  
The class diagram of Exception is as follows:-
Java Exceptions
This discussion is regarding why we have Runtime type of exceptions in Java.

When exception was first designed, there was worry about conditions that occur because of

  • problems with the machine on which the program being run
  • bugs in VM 
  • security violations.
These sort of things can occur any time. But it would only clutter up the language to require every method declare that it can throw an exception for these reasons.
Generally, it would not be the method that throw such an exception, it would be the underlying VM.  It would just make the code longer and less robust  To clear this , a special sort of exception, called RunTime Exception was build into the Java. Since RunTimeException can be thrown any time and need not to be declared as part of method signature.

Use: Sometime in the midst of writing the implementation  programmer discovers that there is an exception condition that hadn't thought in the original design. One way , is to declare the exception as Exception as subclass of Runtime Exception. That way, one need not to change the calling code.
But , it will result more work and headache in future, when you try to find out why system is failing, Since it not mandate to catch.....

Comments

Popular posts from this blog

When to use getClass() and instanceOf in Java

Why Time gets truncated with ResutSet getDate() function.

How class.forName loads the Database Driver in JDBC