001 /** 002 * Exercise 9.11 from the book 003 * 004 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 005 * @version 1.0 006 */ 007 public class Exercise9_11 { 008 /** 009 * Main that catches exceptions 010 */ 011 public static void main(String[] args) { 012 try { 013 ExtendedClass c1 = new ExtendedClass(34, "Hello"); 014 System.out.println(c1); 015 } catch (SomeException e) { 016 System.out.println(e); 017 } 018 try { 019 ExtendedClass c2 = new ExtendedClass(36, "Hello again"); 020 System.out.println(c2); 021 } catch (SomeException e) { 022 System.out.println(e); 023 } 024 } 025 } 026 027 /** 028 * 029 * A simple exception class used. 030 * 031 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 032 * @version 1.0 033 */ 034 035 class SomeException extends Exception { 036 } 037 038 /** 039 * Root in inheritance hierarchy. 040 * 041 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 042 * @version 1.0 043 */ 044 045 class BaseClass { 046 String s; 047 048 BaseClass(String init) throws SomeException { 049 if (init.length() > 5) 050 throw new SomeException(); 051 this.s = init; 052 } 053 054 public String toString() { 055 return "s : " + s; 056 } 057 } 058 059 /** 060 * A subclass. 061 * 062 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 063 * @version 1.0 064 */ 065 066 class ExtendedClass extends BaseClass { 067 int i; 068 069 ExtendedClass(int i, String s) throws SomeException { 070 super(s); 071 this.i = i; 072 } 073 074 public String toString() { 075 return "s : " + s + " i: " + i; 076 } 077 } 078