//import java.util.*; class Exception1{ public static void main(String args[]) { try{ AClass ac = new AClass(); ac.setAge(18); System.out.println(ac.toString()); try{ exTest(-18); }catch(IllegalArgumentException ie) { ie.printStackTrace(); }/*catch(AgeLessThanZeroException ae) { ae.printStackTrace(); }//catch */ }catch(Exception e){ } }//main public static void exTest(int age) { //try{ if(age < 0) throw new IllegalArgumentException("Has to be positive."); else System.out.println("The age is = " + age); /* }catch(IllegalArgumentException ie){ ie.printStackTrace(); }//catch */ }//exText() }//Exception1 class AClass{ private int age; AClass(){} AClass(int age){ this.age = age; }//constructor public void setAge(int newage)/* throws AgeLessThanZeroException*/{ if(newage < 0){ throw new AgeLessThanZeroException("Age cannot be less than zero."); }//if else this.age = newage; }//setAge public String toString(){ return "Age = " + age; }//toString() }//AClass class AgeLessThanZeroException extends RuntimeException{ // if extends Exception --> need to have throws in the method public AgeLessThanZeroException(String message){ super(message); System.out.println(message); } }//AgeLessThanZeroException