001package edu.pdx.cs410J.lang; 002 003/** 004 * This class demonstrates throwing exceptions. It reads two 005 * <code>double</code>s from the command line and divides the second 006 * by the first. If the second is zero, an 007 * <code>IllegalArgumentException</code> is thrown. 008 */ 009public class DivTwo { 010 011 /** 012 * Reads two <code>double</code>s from the command line and divides 013 * the first by the second. 014 */ 015 public static void main(String[] args) { 016 double d1 = 0.0; 017 double d2 = 0.0; 018 019 if (args.length < 2) { 020 System.err.println("Not enough arguments"); 021 System.exit(1); 022 } 023 024 try { 025 d1 = Double.parseDouble(args[0]); 026 d2 = Double.parseDouble(args[1]); 027 028 } catch (NumberFormatException ex) { 029 System.err.println("Not a double: " + ex); 030 System.exit(1); 031 } 032 033 if (d2 == 0.0) { 034 String m = "Denominator can't be zero!"; 035 throw new IllegalArgumentException(m); 036 } 037 038 System.out.println(d1 + " / " + d2 + " = " + (d1/d2)); 039 } 040 041}