Home  Listings  By Name  By Subject  Email  www.tomswan.com  Help 

Java 2 Just Click! Solutions

Chapter 7 Listings

© 2001 by Tom Swan. All rights reserved. Updated: 6/4/01 12:20:54 PM

Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page


Listing 7-1 ExceptDemo/ExceptDemo.java Page 105

Return to top
001: class NewMathException extends Exception {
002:  // Constructor
003:  public NewMathException(double b, double e) {
004:   super("Domain error: base = " + b + " exp = " + e);
005:  }
006: }
007: 
008: final class NewMath {
009:  // Prevent instantiation of class
010:  private NewMath() { }
011:  // Return b raised to the power of e
012:  public static double power(double b, double e) 
013:  throws NewMathException {
014:   NewMathException error = new NewMathException(b, e);
015:   if (b > 0.0) return Math.pow(b, e);
016:   if (b < 0.0) {
017:    Double d = new Double(e);
018:    double ipart = d.intValue();
019:    double fpart = e - ipart;
020:    if (fpart == 0) {
021:     if ((ipart % 2) != 0)  // i.e. ipart is odd
022:      return -Math.pow(-b, e);
023:     else
024:      return Math.pow(-b, e);
025:    } else
026:     throw error;
027:   } else {
028:    if (e == 0.0) return 1.0;
029:    if (e < 1.0) throw error;
030:    return 0.0;
031:   }
032:  }
033: }
034: 
035: class ExceptDemo {
036:  public static void main(String args[]) {
037:   if (args.length < 2) {
038:    System.out.println("Specify value and exponent");
039:    System.out.println("ex. java ExceptDemo -4 1.5");
040:   } 
041:   else 
042:   try {
043:    double base = new Double(args[0]).doubleValue();
044:    double exponent = new Double(args[1]).doubleValue();
045:    double result = NewMath.power(base, exponent);
046:    System.out.println("Result = " + result);
047:   } catch (NewMathException e) {
048:    System.out.println(e.getMessage());
049:   }
050:  }
051: }
Return to top

Listing 7-2 FinallyDemo/FinallyDemo.java Page 109

Return to top
001: // Exception class
002: class ThrowMe extends Exception {
003:  ThrowMe() { }
004:  ThrowMe(String s) {
005:   super(s); 
006:  }
007: }
008: 
009: class FinallyDemo {
010:  // Test method -- pass 0, 1, or 2 for different exceptions
011:  static void testMethod(int n) throws Exception, ThrowMe {
012:   switch (n) {
013:   case 1: 
014:    throw new Exception("Unhandled exception");
015:   case 2:
016:    throw new ThrowMe("To the wolves");
017:   default:
018:    return;
019:   }
020:  }
021:  // Main program 
022:  public static void main(String args[]) 
023:   throws Exception {
024:   int argument = 0;
025:   if (args.length > 0)
026:    argument = Integer.parseInt(args[0]);
027:   try {
028:    testMethod(argument);
029:   } catch (ThrowMe e) {
030:    System.out.println("ThrowMe: " + e.getMessage());
031:   } finally {
032:    System.out.println("Finally statement");
033:   }
034:   System.out.println("Statement after try block");
035:  }
036: }
Return to top

Listing 7-3 NestedTry/NestedTry.java Page 111

Return to top
001: class NewException extends Exception { }
002: class NewNewException extends NewException { }
003: 
004: class NestedTry {
005:  public static void test() throws NewNewException {
006:   throw new NewNewException();
007:  }
008:  public static void main(String args[]) {
009:   try {
010:    try {
011:     test();
012:    } catch (NewNewException e) {
013:     System.out.println("Inner try block exception caught");
014:     throw e;  // Rethrow exception
015:    }
016:   } catch (NewException e) {
017:    System.out.println("Outer try block exception caught");
018:   }
019:  }
020: }
Return to top

Listing 7-4 StackTrace/StackTrace.java Page 113

Return to top
001: class NewException extends Exception { }
002: 
003: class StackTrace {
004:  // Cause an exception to be thrown
005:  public static void test() throws NewException {
006:   throw new NewException();
007:  }
008:  // Main program--catch the thrown exception
009:  public static void main(String args[]) {
010:   try {
011:    test();
012:   } catch (NewException e) {
013:    System.out.println("NewException caught. Tracing stack:");
014:    e.printStackTrace();  // Trace exception origin
015:   }
016:  }
017: }
Return to top