Saturday, 12 July 2014

Topic : Exception Handling in java - 12 July 2014. Topic not yet complete.

Exception Handling hierarchy:




Error :  Example are OutOfMemoryError, StackOverflowError. out of scope of application.
CheckedException : Force to handle in try catch block or propagate using throws method signature.
RuntimeException : If we are throwing any runtime exception in a method, it’s not required to specify them in the method signature throws clause. due to bad programming.


Question : How to define custom runtime exception ?
Answer :
public class EmptyStackException extends RuntimeException {
 public EmptyStackException() { super(); }
 public EmptyStackException(String s) { super(s); }
 public EmptyStackException(String s, Throwable throwable) { super(s, throwable); }
 public EmptyStackException(Throwable throwable) { super(throwable); }
}

Question: How to define custom checked exception ?
Answer : public class ValidationException extends Exception { }



Question: How to convert Checked exception into Runtime Exception ?
Answer :  

Step 1 .Define your own custom Runtime exception classes.
Step 2: catch the exception in try catch block, catch the checked exception.
Step 3: throw exception in catch block, using your custom runtime exception.

Question : Throwable class ?

Answer :
public class Throwable
              extends Object
                       implements Serializable
Constructor
S.N.
Constructor & Description
1
Throwable()
This constructs a new throwable with null as its detail message.
2
Throwable(String message)
This constructs a new throwable with the specified detail message.
3
Throwable(String message, Throwable cause)
This constructs a new throwable with the specified detail message and cause.
4
Throwable(Throwable cause)
This constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

Class methods


S.N.
Method & Description
1
This method fills in the execution stack trace.
2
This method returns the cause of this throwable or null if the cause is nonexistent or unknown.
3
This method creates a localized description of this throwable.
4
This method returns the detail message string of this throwable.
5
This method provides programmatic access to the stack trace information printed by printStackTrace().
6
This method initializes the cause of this throwable to the specified value.
7
This method prints this throwable and its backtrace to the standard error stream.
8
This method prints this throwable and its backtrace to the specified print stream.
9
This method prints this throwable and its backtrace to the specified print writer.
10
This method sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods.
11
This method returns a short description of this throwable.


Question : Compile time exception ??
Answer :
For example, ClassNotFoundException, IllegalAccessException, NoSuchMethodException etc..

  • JVM enforces to handle or catch it: Any exception which is must to handle or catch it, while writing the program is called compile time exception.
  • If your code fails to either handle a compile time exception or declare that it is thrown, your code won't compile.