Sponsored By
















Throw 예외처리 하는 방법임.

 

 

 

 

 

 

 

ㅁ. 간단할 설명.

'divide();' 에서 일을 다 처리하기 때문에 정작 메인에서는 알 수가 없다.

따라서, 'divide2();' 에서 작업을 시킨다음에 그 작업내역을 메인으로 던져서(throw) 메인에서 일(TryCatch)을 할 수 있게 한다.

 

 

 

 

 

 

 

00. 'divide(){}' 를 평소처럼 만들고 'divide2(){}' 는 만들때 throws Exception을 써서 만들어보자.

     하지만, 메인메소드에 적을때에는 'divide2();' 를 try~catch 내부에 넣어야 함을 잊지말자.







01. 결과값.

 

 

 

 

 

 

※. 코드

 

 

public class Test {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

divide();

 

try {

divide2();

} catch (Exception e) {

// TODO: handle exception

System.out.println("main Reason : " + e.getMessage());

}

 

}

 

public static void divide2() throws Exception

{

int a = 2;

int b = 0;

int result;

 

result = a/b;

System.out.println("result = " + result);

}

 

 

public static void divide()

{

int a = 2;

int b = 1;

int result;

 

try {

result = a/b;

System.out.println("result = " + result);

} catch (Exception e) {

// TODO: handle exception

System.out.println("Reason : " + e.getMessage());

}

 

 

}

 

 

}

 



Sponsored By















+ Recent posts