3

Suppose in your program you might get an IndexOutOfBoundsException. i am handling it in the following way:

try{
//throws an IndexOutOfBoundsException during runtime
}catch(IndexOutOfBoundsException ex){
System.err.println(ex);
}

This will only display java.lang.IndexOutOfBoundsException. But I would like to display a detailed error message (which won't terminate the program), like the one that java gives us (lineNumber, fileName, etc) when we do not handle the error and thus terminates the program.

4 Answers4

3

Use ex.printStackTrace() method to print the exception:

try {
    int[] x = new int[1];
    x[2] = 5;
} catch (IndexOutOfBoundsException ex) {
    ex.printStackTrace();
}
System.err.println("Program completed successfully");

Demo.

If you are running in an environment where console output is not desirable, call ex.getStackTrace(), and display elements in a way that is consistent with the user interface of your program.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • this was very helpful brother :) but this prints out too much information. I would like the error message to be like the one java gives. java.lang.ArrayIndexOutOfBoundsException: 6 at sth.main(sth.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:266) – Arafe Zawad Sajid Nov 01 '16 at 03:05
  • Found out how to display the above mentioned lines. :D – Arafe Zawad Sajid Nov 01 '16 at 09:56
3

In Java you can use printStackTrace on any exception object to get the stack trace printed by Java. In your case a minimal:

try {
    // Throw an IndexOutOfBoundsException
} catch (IndexOutOfBoundsException ex) {
    ex.printStackTrace();
}

This prints the stack trace to System.err. You can also pass it a print stream or even System.out to print to that particular stream.

Additionally, if you use java logger, you can use:

logger.log(<LOG_LEVEL>, <LOG_MESSAGE>, ex);

to log the exception. For more details see: https://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html

vivekprakash
  • 346
  • 2
  • 9
0

You can use

ex.GetMessage()

Thanks

Jose Gomez
  • 11
  • 1
0

Got this from one of my teachers. Might be helpful for other beginners like me. (This is not a part of any h.w./assignment/etc. Curiosity.)

public class ExceptionDemo{
public static void main(String[] args){
    int[] array = new int[2];
    try {
        System.out.println(array[100]);//non-existent
    } catch (IndexOutOfBoundsException ex){
        StackTraceElement[] e = ex.getStackTrace();
        System.err.println("Got error= " + ex + "\n"+
                           "in file "+e[0].getFileName() +"\n"+
                           "in class "+e[0].getClassName() +"\n"+
                           "in method "+e[0].getMethodName() +"\n"+
                           "in line "+e[0].getLineNumber());
        System.err.println("Full trace= ");
        ex.printStackTrace(System.err);
    }
    System.out.println("As Salamu Alaikum");
  }
}