/** * example3.java - an example of handling a checked (Divide by 0) exception * @author Richard S. Huntrods * @date June 13, 2001 * @version 1.0 * */ public class Example3 { private int myNumerator; private int myDenominator; private int myAnswer; Example3(int myNumerator, int myDenominator) { this.myNumerator = myNumerator; this.myDenominator = myDenominator; this.myAnswer = 0; } public void calculate() { try { myAnswer = myNumerator / myDenominator; System.out.println(myNumerator + " / " + myDenominator + " = " + myAnswer); } catch(Exception e) { System.out.println("Handled exception: " + e.getMessage()); e.printStackTrace(); System.exit(0); } } public static void main(String[] args) { Example3 ex3 = new Example3(5, 0); // here's where we set up 5/0... ex3.calculate(); // here's where we trigger the exception... } }