Beispiel 12  [Division durch Null]  
 
 
 
class Div {
  public  static int  division (int z, int n) {
    return z/n;                                        // Zeile 3
  };
};
public class Main {
  public static void main (String[] args) {
    int nenner = 0;
    int zaehler = 0;
    System.out.println(Div.division(zaehler, nenner)); // Zeile 11
  };
  
};
 
 Ergibt als Ausgabe folgende Aufrufstruktur auf dem Stack:
java.lang.ArithmeticException: / by zero
        at Div.division(Main.java:3)
        at Main.main(Main.java:11)
Process Main exited abnormally with code 1
Die Ausnahme ist eine Instanz von ArithmeticException, einer
 Klasse aus aus java.lang.
 Beispiel 13  [Division durch Null (II)]  
 Der folgende Code zeigt, wie man den Divisionsfehler abfangen |
 und hier in eher nicht-empfehlenswerter Weise ignorieren |
 kann:28 
 
 
 
class Div {
  public  static int  division (int z, int n) {
    return z/n;                                     
  };
};
public class Main2 {
  public static void main (String[] args) {
    int nenner   = 0;
    int zaehler  = 0;
    int ergebnis = 0;                                // vorbelegen
    try {
      ergebnis = Div.division(zaehler, nenner); 
    }
    catch (ArithmeticException e) {
      System.out.println("Mir egal, ich rechne weiter");
    };
    System.out.println(ergebnis);
  };
};
 
 Ein klein wenig informativer
 wäre bereits die Ausgabe
  System.out.println(e + ", trotzdem rechne ich weiter");
 Beispiel 14  [Ausnahmen]  
 
 
 
class MyException extends Exception {
  int wert;
  MyException(int i) {           // Konstruktor
    wert = i;
  };
  public int get_wert() {
    return wert;
  };
};
public class Main {
  public static void main (String[] args) {
    int x;
    try 
      {Main.unsafe_method(0);}
    catch (MyException e) {
      System.out.println("MyException mit wert = " + 
    e.get_wert());};
  };
  public static void unsafe_method(int i) throws MyException {
    if (i == 0)   {throw (new MyException (i));}
    else System.out.println("OK");
  };
};