문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:exception [2014/01/22 13:06] kwon37xi [Checked Exception] |
java:exception [2014/01/22 13:21] kwon37xi [Checked Exception] |
||
---|---|---|---|
줄 3: | 줄 3: | ||
===== Checked Exception ===== | ===== Checked Exception ===== | ||
가능하면 사용하지 말 것. | 가능하면 사용하지 말 것. | ||
- | * [[http:// | + | * [[http:// |
- | class Thrower { | + | |
- | public static void spit(final Throwable exception) { | + | |
- | class EvilThrower< | + | |
- | @SuppressWarnings(" | + | |
- | private void sneakyThrow(Throwable exception) throws T { | + | |
- | throw (T) exception; | + | |
- | } | + | |
- | } | + | |
- | new EvilThrower< | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public class ThrowerSample { | + | |
- | public static void main( String[] args ) { | + | |
- | Thrower.spit(new RemoteException(" | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
* [[http:// | * [[http:// | ||
- | public | + | public class ExceptionThrower |
- | private | + | private |
- | public static void throwUnchecked(final | + | public static void spit(final |
- | // 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! | + | |
- | | + | |
} | } | ||
- | | + | |
- | * Remember, Generics are erased in Java. So this basically throws an Exception. The real | + | |
- | * Type of T is lost during the compilation | + | // 아래 Type Casting 정보는 Java Generic의 특성상 컴파일시 정보가 사라지므로 |
- | */ | + | // 실제 캐스팅은 발생하지 않는다. |
- | | + | throw (T) throwable; |
- | // Since the type is erased, this cast actually does nothing!!! | + | |
- | // we can throw any exception | + | |
- | throw (T) toThrow; | + | |
} | } | ||
} | } | ||
+ | |||
+ | ExceptionThrower.spit(new IOException(" | ||
</ | </ |