Java Exceptions Questions
Java Exceptions Questions
User defined Exceptions are custom Exception classes defined by the user for specific purpose. A
user defined exception can be created by simply sub-classing an Exception class or a subclass of
an Exception class. This allows custom exceptions to be generated (using throw clause) and
caught in the same way as normal exceptions.
Example:
A catch clause can catch any exception that may be assigned to the Throwable type. This
includes the Error and Exception types. Errors are generally irrecoverable conditions
Error's are irrecoverable exceptions. Usually a program terminates when an error is encountered.
The throw keyword denotes a statement that causes an exception to be initiated. It takes the
Exception object to be thrown as an argument. The exception will be caught by an enclosing try-
catch block or propagated further up the calling hierarchy. The throws keyword is a modifier of a
method that denotes that an exception may be thrown by the method. An exception can be
rethrown.
Throwable
A checked exception is some subclass of Exception (or Exception itself), excluding class
RuntimeException and its subclasses. Making an exception checked forces client programmers
to deal with the exception may be thrown. Checked exceptions must be caught at compile time.
Example: IOException.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its
subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't
force client programmers either to catch the exception or declare it in a throws clause. In fact,
client programmers may not even know that the exception could be thrown. Example:
ArrayIndexOutOfBoundsException. Errors are often irrecoverable conditions.
Does the code in finally block get executed if there is an exception and a return statement
in a catch block?
Or
The finally clause is used to provide the capability to execute code no matter whether or not an
exception is thrown or caught. If an exception occurs and there is a return statement in catch
block, the finally block is still executed. The finally block will not be executed when the
System.exit(0) statement is executed earlier or on system shut down earlier or the memory is
used up earlier before the thread goes to finally block.
try{
//some statements
}
catch{
//statements when exception is caught
}
finally{
//statements executed whether exception occurs or not
}
Does the order of placing catch statements matter in the catch block?