001 /** 002 * Exercise 9.7 from the book. 003 * 004 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 005 * @version 1.0 006 */ 007 public class Exercise9_7 { 008 /** 009 * Throws various exceptions 010 */ 011 public void evaluateNumber(int i) throws TooSmallException, 012 TooLargeException, DoesNotLikeNumberException { 013 if (i < 0) 014 throw new TooSmallException("Too small: " + i); 015 else if (i > 100) 016 throw new TooLargeException("Too large " + i); 017 else if ((i % 2) == 0) 018 throw new DoesNotLikeNumberException("Do not like evens " + i); 019 else 020 System.out.println("Got " + i); 021 } 022 023 /** 024 * Calls methods and catchtes exceptions 025 */ 026 public static void main(String[] args) { 027 Exercise9_7 o = new Exercise9_7(); 028 for (int i = -2; i < 110; i += 7) { 029 try { 030 o.evaluateNumber(i); 031 } catch (Exception e) { 032 System.out.println("Exception " + e.getMessage()); 033 } 034 } 035 } 036 } 037 038 /** 039 * Exception class 040 * 041 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 042 * @version 1.0 043 */ 044 045 class TooSmallException extends Exception { 046 TooSmallException() { 047 super(); 048 } 049 050 TooSmallException(String s) { 051 super(s); 052 } 053 } 054 055 /** 056 * Exception class 057 * 058 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 059 * @version 1.0 060 */ 061 062 class TooLargeException extends Exception { 063 TooLargeException() { 064 super(); 065 } 066 067 TooLargeException(String s) { 068 super(s); 069 } 070 } 071 072 /** 073 * Exception class 074 * 075 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 076 * @version 1.0 077 */ 078 079 class DoesNotLikeNumberException extends Exception { 080 DoesNotLikeNumberException() { 081 super(); 082 } 083 084 DoesNotLikeNumberException(String s) { 085 super(s); 086 } 087 }