Java – print exception as string examples

Print exception stack trace

public class Main {
  public static void main(String[] args) {
    try {
      float f = 1/0;
    } catch (Exception e) {
      e.printStackTrace(System.out);
    }
  }
}
OpenJDK 64-Bit Server VM warning: Insufficient space for shared memory file:
   1728
Try using the -Djava.io.tmpdir= option to select an alternate temp location.

Print exception short description

public class Main2 {
  public static void main(String[] args) {
    try {
      float f = 1/0;
    } catch (Exception e) {
      System.out.println(e);  // or System.out.println(e.toString()); 
    }
  }
}
OpenJDK 64-Bit Server VM warning: Insufficient space for shared memory file:
   1740
Try using the -Djava.io.tmpdir= option to select an alternate temp location.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments