ex1) exception handle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public static void main(String [] args) { TestApplication test = new TestApplication(); test.A(); } private void A() { B(); } private void B() { //2, 넘어온 Exception을 try-catch 처리 해 준다. try { C(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void C() throws Exception { //1,throws를 통해 호출하는 메소드로 exception handling을 미룬다. throwable(); } private void throwable() throws Exception { throw new Exception( "I'm exception!" ); } |
ex2) Runtime exception의 경우, 명시적으로 throws를 사용하지 않아도 문제가 되지 않는다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public static void main(String [] args) { TestApplication test = new TestApplication(); test.A(); } private void A() { B(); } private void B() { //2, 넘어온 Exception을 try-catch 처리 해 준다. try { C(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void C() /* throws RuntimeException */ { //1,throws를 통해 호출하는 메소드로 exception handling을 미룬다. throwable(); } private void throwable() throws RuntimeException { throw new RuntimeException( "I'm exception!" ); } |
출처 : 자바의 신
'java' 카테고리의 다른 글
Nested class의 개념 (0) | 2019.09.15 |
---|---|
StringBuilder, StringBuffer, CharSequence 용도 (0) | 2019.09.15 |
Throwable 및 stack trace의 이해 (0) | 2019.09.15 |
try-catch finally (0) | 2019.09.15 |
enum class 활용법 (0) | 2019.09.14 |