Reputation: 3589
For some reason I am missing part of the stack trace, the actual root of the exception.
public void method1(){
try{
method2();
}
catch(SearchException e){
logger.error("Search error", e.getMessage(), e);
}
}
private void method2(){
try{
//Logic which potentially can throw exception
....
....
}
catch(Exception e){
throw new SearchException("Error during execution Module A", e);
}
}
public class SearchException extends Exception{
public SearchException(String message, Throwable cause) {
super(message, cause);
}
}
Anyone knows a reason why I am missing part of stacktrace where fist place exception happened? Could you suggest a correct way of handling/logging exceptions?
Upvotes: 0
Views: 103
Reputation: 41240
super(message, cause);
The message
will be the detailMessage
of Throwable
class. And it will be used in toString
method and source cause(Exception) will be ignored.
358 public String toString() {
359 String s = getClass().getName();// class Name
360 String message = getLocalizedMessage();// detailMessage
361 return (message != null) ? (s + ": " + message) : s;
362 }
You may override the toString method in custom Exception
class.
class SearchException extends Exception{
String str=null;
public SearchException(String str,Throwable th){
super( th);
this.str=str;
}
public String toString(){
return super.toString() +" - "+ str;
}
}
This customized form will print -
Search error -----java.lang.NullPointerException-------SearchException: java.lang.NullPointerException - Error during execution Module A
Upvotes: 1