문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:exception [2014/01/22 12:58] kwon37xi 새로 만듦 |
java:exception [2014/01/22 13:20] kwon37xi [Checked Exception] |
||
---|---|---|---|
줄 3: | 줄 3: | ||
===== Checked Exception ===== | ===== Checked Exception ===== | ||
가능하면 사용하지 말 것. | 가능하면 사용하지 말 것. | ||
- | * [[http:// | + | * [[http:// |
- | class Thrower | + | * [[http:// |
- | public static void spit(final Throwable | + | public |
- | | + | private ExceptionThrower() {} |
- | @SuppressWarnings(" | + | |
- | private void sneakyThrow(Throwable | + | public static void spit(final Throwable |
- | throw (T) exception; | + | |
- | } | + | |
- | | + | } |
- | | + | |
+ | | ||
+ | private | ||
+ | // 아래 Type Casting 정보는 Java Generic의 특성상 컴파일시 정보가 사라지므로 | ||
+ | | ||
+ | | ||
} | } | ||
} | } | ||
- | public class ThrowerSample | + | public |
- | public static void main( String[] args ) { | + | private UncheckedThrow(){} |
- | | + | |
+ | public static void throwUnchecked(final Exception ex){ | ||
+ | | ||
+ | // 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 | ||
+ | // 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; | ||
} | } | ||
} | } | ||
</ | </ | ||
- | * [[http:// |