문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
java:exception [2014/01/22 13:20] kwon37xi [Checked Exception] |
java:exception [2021/07/14 12:49] (현재) kwon37xi |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== Java Exception ====== | ====== Java Exception ====== | ||
| + | * [[http:// | ||
| + | * [[java: | ||
| ===== Checked Exception ===== | ===== Checked Exception ===== | ||
| 가능하면 사용하지 말 것. | 가능하면 사용하지 말 것. | ||
| * [[http:// | * [[http:// | ||
| - | * [[http:// | + | * [[http:// |
| public class ExceptionThrower { | public class ExceptionThrower { | ||
| private ExceptionThrower() {} | private ExceptionThrower() {} | ||
| 줄 21: | 줄 23: | ||
| } | } | ||
| - | public final class UncheckedThrow { | + | ExceptionThrower.spit(new IOException(" |
| - | private UncheckedThrow(){} | + | |
| - | + | ||
| - | public static void throwUnchecked(final Exception ex){ | + | |
| - | // Now we use the ' | + | |
| - | // from the parameters. However you can specify the type also explicit! | + | |
| - | // Now we du just that! We use the RuntimeException as type! | + | |
| - | // That means the throwsUnchecked throws an unchecked exception! | + | |
| - | // Since the types are erased, no type-information is there to prevent this! | + | |
| - | UncheckedThrow.< | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * Remember, Generics are erased in Java. So this basically throws an Exception. The real | + | |
| - | * Type of T is lost during the compilation | + | |
| - | */ | + | |
| - | public static <T extends Exception> | + | |
| - | // Since the type is erased, this cast actually does nothing!!! | + | |
| - | // we can throw any exception | + | |
| - | throw (T) toThrow; | + | |
| - | } | + | |
| - | } | + | |
| </ | </ | ||