Citrix System Inc Interview Question
Software Engineer / DevelopersTeam: Shared file
Country: United States
Interview Type: Phone Interview
When an exception occurs and if re-throwing the same exception, then stackTrace would be still maintained. Unless we create a new exception object, the stack trace of the exception from callee method still preserves.
public class RethrowException {
void method1() throws Exception{
throw new Exception("exception message");
}
void method2() throws Exception{
try{
method1();
}catch(Exception exception){
throw exception;
}
}
public static void main(String[] args) throws Exception{
RethrowException re = new RethrowException();
re.method2();
}
}
Question is very vague... It actually depends on the way you are re-throwing the exception. If you re-throw the caught exception as a inner exception parameter to constructor of new exception, original stack trace is maintained. (Note:Not all exception constructors support inner exceptions). But if you create new exception and re throw new exception, the original caught exception's stack trace is lost.
This is what I know...
Example : we have DAO in that one method accesses database and can throw SQLEXcetion & another method reads a file can throw FileNotFoundException & another method uses WebServices can throw RemoteException .If in there try catch block we rethrow a generalized exception with correct message then the method interacting with the DAO method's need not to handle different type of exceptions once handling of wraped generalized exception will be enough
My interpretation of question is that instead of handling the exception after catching it, what happens when we re-throw t again.... The answer for that it will go up the method call hierarchy and be eventually handled by JVM (you will stack trace once it reaches at that point)
- ??? January 16, 2014