Piyush Chaudhari
Piyush Chaudhari

Reputation: 1012

Java exception handling get console error message

I want to get error message using java when exception are generated.

now I have java code with following scenario:

method first(){

     try{
        second();
     }catch(Exception e){
        System.out.println("Error:> "+e)
     }

}

method second(){

     try{
       my code
     }catch(Exception e){
        throw new Exception("Exception generate in second method",e);
     }

}

now when the first method execute then I get only "Exception generate in second method" message but there is some other message printed on console by java so how to get that console error message.

Note: I have already try with e.getMessage(); and e.printStackTrace();

Upvotes: 0

Views: 13505

Answers (5)

techPackets
techPackets

Reputation: 4516

I believe this is the console message you want to achieve:

Error:> java.lang.Exception: Exception generate in second method

Try this code, when the catch block of the second method throws an exception the second method should declare it as throws or put a nested try catch within the catch block.

The exception is propagated to the first() method which is handled by its catch block.

 public class Test {

        public void first() {

            try {
                second();
            } catch (Exception e) {
                System.out.println("Error:> " + e);
            }

        }

        public void second() throws Exception {

            try {
                throw new Exception();
            } catch (Exception e) {
                throw new Exception("Exception generate in second method", e);
            }
        }

        public static void main(String ars[]) {

            Test test = new Test();

            test.first();

        }
    }

Upvotes: 0

Rob Meeuwisse
Rob Meeuwisse

Reputation: 2937

Every exception has a cause that you can get with getCause(). You can go recursively down them until you get to the root cause. Here is your example with a utility that dumps the exception with all its causes like the console does.

private void first() {
    try  {
        second();
    } catch (Exception ex) {
        Log.e("CATCH", getExceptionDump(ex));
    }
}

private void second() {
    try {
        throw new UnsupportedOperationException("We don't do this.");
    } catch (Exception ex) {
        throw new RuntimeException("Exception in second()", ex);
    }
}

private String getExceptionDump(Exception ex) {
    StringBuilder result = new StringBuilder();
    for (Throwable cause = ex; cause != null; cause = cause.getCause()) {
        if (result.length() > 0)
            result.append("Caused by: ");
        result.append(cause.getClass().getName());
        result.append(": ");
        result.append(cause.getMessage());
        result.append("\n");
        for (StackTraceElement element: cause.getStackTrace()) {
            result.append("\tat ");
            result.append(element.getMethodName());
            result.append("(");
            result.append(element.getFileName());
            result.append(":");
            result.append(element.getLineNumber());
            result.append(")\n");
        }
    }
    return result.toString();
}

Upvotes: 1

David ten Hove
David ten Hove

Reputation: 2826

You can print the cause of the exception you get. Try this:

method first(){

     try{
        second();
     }catch(Exception e){
        System.out.println("Error:> "+e);
        if (e.getCause() != null) {
            System.out.println("Cause:> " + e.getCause());
        }
     }

}

Upvotes: 0

Kraiss
Kraiss

Reputation: 999

Why you cannot use print stack trace ?

Because A throwable contains a snapshot of the execution stack of its thread at the time it was created. (see Throwable)

It implies that, if you want to print the stack trace you need to use the printStackTrace() method BUT in your second method !

method second(){
  try {
    my code
  } catch(Exception e) {
    e.printStackTrace();
    throw new Exception("Exception generate in second method",e);
  }
 }

Or using a the tricky method setStackTrace and using the printStackTrace() in first

method second(){
  try {
    my code
  } catch(Exception e) {
    Exception ex = new Exception("Exception generate in second method",e);
    ex.setStackTrace(e);
    throw ex;
  }
 }

method first(){

 try {
   second();
 } catch(Exception e) {
    e.printStackTrace();
 }
}

Upvotes: 0

Atul Sharma
Atul Sharma

Reputation: 728

The message in the Exception constructor argument is not printed in the exception detail. You can simply use this code to print the message :

method first(){

     try{
        second();
     }catch(Exception e){
        System.out.println("Error:> "+e.getMessage())
     }

}

Hope this solves your problem

Upvotes: 0

Related Questions