Exceptions - Musings

By: Nicholas Duchon.


Exceptions? Here is a quick introduction to the key points. 


Code Example:

public class DivZeroTest {
  public static void main (String [] args) {
    try {
      int k = 1, j = 0;
      System.out.println ("Div: " + k/j);
    } catch (ArithmeticException e) {
      System.out.println ("Exception: " + e);

      System.out.println ("\n***** e.getMessage ():");
      System.out.println ("Exception: " + e.getMessage());

      System.out.println ("\n***** Thread.dumpStack ():");
      Thread.dumpStack ();

      System.out.println ("\n***** e.printStackTrace ():");
      e.printStackTrace ();
    } // end try/catch
  } // end main
} // end class DivZeroTest

Output:

Exception: java.lang.ArithmeticException: / by zero

***** e.getMessage ():
Exception: / by zero

***** Thread.dumpStack ():
java.lang.Exception: Stack trace
    at java.lang.Thread.dumpStack(Thread.java:1329)
    at DivZeroTest.main(DivZeroTest.java:11)

***** e.printStackTrace ():
java.lang.ArithmeticException: / by zero
    at DivZeroTest.main(DivZeroTest.java:5)


ND.