ex : public void go() throws AException, BException{}
메소드 overriding 복습
-하위 class에서 부모의 메소드 재정리
◇규칙-전제 : 이름 통일
① return Type, 매개변수가 동일
② 하위의 접근제한자가 상위것과 같거나 더 넓어야 한다.
③ 부모가 throws한 것만 할 수 있다.
- 부모에서 throws 한것을 안할 수 있다.
Super클래스
public void go() throws AException{
~~~~}
Sub클래스
public void go(){}
가능하다
public void go() throws AException, BException
불가능하다, 더 던지는것은 불가능하며, 최상위 부모는 보지 않고 바로 위 부모만 확인해야 한다.
public void go() throws A1Exception, A2Exception
부모타입이라면 던질수 있다.
try, catch, finally의 사용 try{ // 오류가 발생 할지 안 할지 확실치 않으므로 일단 시도 (try)
오류(예외)가 발생할 가능성이 있는 코드
}
catch(예외타입 e){ // 예외타입에 맞는 예외가 발생한다면 잡는다 (catch) / 다른 타입이 발생한다면 못 잡는다.
처리코드
}
~try내부에 여러 코드를 일괄 처리 할 수 있지만 Exception이 발생하면 Exception을 처리하고 try/catch밖의 실행구문을 실행한다.
public class ExceptionTest1 {
public static void main(String[] args) {
//uncheck 계열의 Exception
//ArrayIndexOutOfBoundsException - 배열의 index범위를 넘었을때 발생
//NullPointerException - null값을 가진 변수의 instance멤버 호출시 발생
//ArithmeticException - 산술 연산상 문제 발생시 발생(0으로 나눈 경우)
String str = null;
try{System.out.println(str.concat("def"));//null포인터 인셉션 일어남//100%
}catch(NullPointerException ne){
System.out.println("NullPointerException 1번);
}
try{System.out.println(10/0);
}catch(ArithmeticException eee){
System.out.println("ArithmeticException 2번");
}try{
int[] arr={10,20,30};
System.out.println(arr[10]);
}catch(ArrayIndexOutOfBoundsException eeee){
System.out.println("ArrayIndexOutOfBoundsException 3번");
}
System.out.println("메소드 종료");
}
}
<결과창>
예외 처리 없이 정상적으로 실행시 1번 구문에 있는 try문 안에 있는 구문 종료후 catch문을 건너뛰고 2번으로 가고 2번 도 이상없으면 3번으로 이동후 3번도 이상없으면 System.out.println("메소드 종료") 구문을 출력하고 종료되어야 한다.
하지만 위 소스는 모든 것에 예외처리를 주었다. 또한 각각 try-catch구문을 따로 해놓아 예외처리를 따로따로 처리한 다.
<금일 진행한 소스코드>
public class ExceptionTest2 {
public static void main(String[] args) {
String str = "sd";
try{
str.concat("def");
int result=10/10;
int arr[]={10,20,30};
System.out.println(arr[1]);
int i = Integer.parseInt("abced");
}catch(NullPointerException e){
System.out.println("NullPointerException");
}catch(ArithmeticException ee){
System.out.println("ArithmeticException");
}catch(ArrayIndexOutOfBoundsException eee){
System.out.println("ArrayIndexOutOfBoundsException");
}catch(Exception ex){
System.out.println("모든 오류가 발생해따");//더 이상 catch문을 받기 귀찮아서 모든걸 다 받기 위해 만듬
}//결과적으로 한개만 나오게 된다.
System.out.println("메소드 종료");
}
}
public class ExceptionTest1 {
public static void main(String[] args) {
//uncheck 계열의 Exception
//ArrayIndexOutOfBoundsException - 배열의 index범위를 넘었을때 발생
//NullPointerException - null값을 가진 변수의 instance멤버 호출시 발생
//ArithmeticException - 산술 연산상 문제 발생시 발생(0으로 나눈 경우)
String str = null;
try{
System.out.println(str.concat("def"));//null포인터 인셉션 일어남//100%
}catch(NullPointerException ne){
System.out.println("NullPointerException 1번");
}
try{
System.out.println(10/0);
}catch(ArithmeticException eee){
System.out.println("ArithmeticException 2번");
}
try{
int[] arr={10,20,30};
System.out.println(arr[10]);
}catch(ArrayIndexOutOfBoundsException eeee){
System.out.println("ArrayIndexOutOfBoundsException 3번");
}
System.out.println("메소드 종료");
}
}
public class Divide {
public int divide(int num1, int num2) throws ZeroDivideException{
//num1를 num2로 나눈값(몫)을 리턴
if(num2==0){//예외상황
throw new ZeroDivideException();//단순객체에서 예외객체로 던져버린다.
}
return num1/num2;
}
}
//Exception클래스 만들기
//0으로 숫자를 나눴을때의 예외상황을 표현하기 위한 클래스
public class ZeroDivideException extends Exception{
public ZeroDivideException(){}//Exception 클래스 형태
public ZeroDivideException(String message){
super(message);
}
}
public class ExceptionTest {
public static void main(String[] args){//던져서 JVM까지 보내짐
Divide d = new Divide();
int result = 0;
try{
result = d.divide(10,0);//throw
}catch(ZeroDivideException ze){
System.out.println("에러에러");
System.out.println("나눈 결과 : "+result);
try {
result = d.divide(10,5);
} catch (ZeroDivideException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("나눈 결과 : "+result);
}
}