34 예외2 - 예외 던지기
throw와 throws
지금까지 예외를 처리하는 방법으로 try...catch...finally를 배웠다. 이외에 다른 방법도 있다. throw를 사용하는 것이다. throw는 예외처리를 다음 사용자에게 넘기는 것이다. 다음 사용자는 누구일까? 코드를 보자.
package org.opentutorials.javatutorials.exception; class B{ void run(){ } } class C{ void run(){ B b = new B(); b.run(); } } public class ThrowExceptionDemo { public static void main(String[] args) { C c = new C(); c.run(); } }
ThrowExceptionDemo.main(클래스 ThrowExceptionDem의 메소드 main)은 C.run의 사용자이다. C.run은 B.run의 사용자이다. 반대로 B.run의 다음 사용자는 C.run이고 C.run의 다음 사용자는 ThrowExceptionDem.main이 되는 셈이다.