There are two types of exceptions: checked and unchecked. Any Throwableis either one or the other.
An example of a checked exception is IOException; probably the most (in) famous exception thrown is NullPointerException.
, throw throws. @Override ( interface, ), , , throws . , / LESS, MORE .
RuntimeException , Error . throws.
, , throw a CustomException interface, throws, CustomException extends RuntimeException, . ( extends RuntimeException, , IllegalArgumentException IndexOutOfBoundsException ).
, , . , , , , . , interface , , interface.
.
- Java 2nd Edition
- 58: .
- 59: .
- 60:
- 61: ,
- 62: , .
"
, CustomException RuntimeException ( ) "". :
@Override public static void someMethod() throws CustomException {
throw new CustomException();
}
try {
someMethod();
} catch (CustomException e) {
handleCustomException(e);
}
, , :
@Override public static void someMethod() {
throw new RuntimeException(new CustomException());
}
try {
someMethod();
} catch (RuntimeException e) {
if (e.getCause() instanceof CustomException) {
handleCustomException((CustomException) e.getCause());
} else {
throw e;
}
}
, . , , , , .