- extensive model of GUI, IO, Locale, Event, Error, and Exception etc.
- Error is different from Exception; checked exceptions must be caught, declared in the throws-clause, or both, by each method that contains code which could throw them
- the unchecked exceptions ClassCastException hierarchy
java.lang.Object
|
+-java.lang.Throwable
|
+-java.lang.Exception
|
+-java.lang.RuntimeException
|
+-java.lang.ClassCastException
- User-Defined Exceptions
java.lang.Object
|
+--java.lang.Throwable
|
+--java.lang.Exception
|
+--java.lang.RuntimeException
|
+--java.lang.IllegalArgumentException
|
+--StudentFormatException
- Example: declare, catch, and throw a dummy user-defined exception
public class MyException extends Throwable {
}
public class ExceptionTestClass {
public void method(String s) throws
MyException { // declared
if ("happyhappy".equals(s)) {
stdOut.println("They are equal.");
} else {
throw new MyException(); // thrown
}
}
public static void main(String[] args) {
try {
new ExceptionTestClass().method("joyjoy");
} catch (MyException me) { // caught
stdErr.println(me.toString());
}
}
}
- not much use in telling you what went wrong in your program
- Example: catching a Digit outside of [0, 9] range
public class DigitOutOfBoundsException extends
NumberFormatException {
public DigitOutOfBoundsException() {}
public DigitOutOfBoundsException(String s) {
super(s);
}
public DigitOutOfBoundsException(int d) {
this("Invalid digit: " + d);
}
}
- class Digit and DigitTester
public class Digit {
int digit;
public Digit(int d) throws
DigitOutOfBoundsException {
setDigit(d);
}
public Digit(String s) throws
NumberFormatException, DigitOutOfBoundsException {
setDigit(Integer.parseInt(s));
}
private void setDigit(int d) throws
DigitOutOfBoundsException {
if (0 <= d && d <= 9) {
digit = d;
} else {
digit = 0;
throw new DigitOutOfBoundsException(d);
}
}
}
public class DigitTester {
public static void main(String[] args) {
try {
Digit d1 = new Digit(5);
} catch(DigitOutOfBoundsException doobe) {
stdErr.println(doobe.toString());
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
try {
Digit d2 = new Digit(25);
} catch(DigitOutOfBoundsException doobe) {
stdErr.println(doobe.toString());
//DigitOutOfBoundsException: Invalid digit: 25
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
try {
Digit d3 = new Digit("4");
} catch(DigitOutOfBoundsException doobe) {
stdErr.println(doobe.toString());
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
try {
Digit d4 = new Digit("24");
} catch(DigitOutOfBoundsException doobe) {
stdErr.println(doobe.toString());
//DigitOutOfBoundsException: Invalid digit: 24
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
try {
Digit d5 = new Digit("abc");
} catch(DigitOutOfBoundsException doobe) {
stdErr.println(doobe.toString());
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
//java.lang.NumberFormatException: abc
}
}
}
- what if you reverse the order of catch statements in the catch-blocks ? why?