try-catch 블록에 대해 공부하고 있습니다. 여기에 우리는 심지어 우리가trycatch 블록에서 NullPointerException이 발생했습니다.
Exception e = new NullPointerException();
을 할당 할 수 있습니다 그리고 BlewIt 클래스는 다시 형의 예외 클래스입니다, 파열() 에 의해 NullPointerException이 던져. 그래서 throw하는 예외는 catch 블록에서 catch해야하지만 그렇지 않습니다.
class BlewIt extends Exception {
BlewIt() { }
BlewIt(String s) { super(s); }
}
class Test {
static void blowUp() throws BlewIt {
throw new NullPointerException();
}
public static void main(String[] args) {
try {
blowUp();
} catch (BlewIt b) {
System.out.println("Caught BlewIt");
} finally {
System.out.println("Uncaught Exception");
}
}
}
출력 :
Uncaught Exception
Exception in thread "main" java.lang.NullPointerException
at Test.blowUp(Test.java:7)
at Test.main(Test.java:11)
그러나이 같은 코드를 작성하는 경우, 그것은 잘 작동 :
try {
blowUp();
} catch (Exception b) {
System.out.println("Caught BlewIt");
} finally {
System.out.println("Uncaught Exception");
}
이제 BlewIt는 NullPointerException이 유형이다하지만 여전히 내가 같은 출력을 얻고있다.
class BlewIt extends NullPointerException {
BlewIt() {
}
BlewIt(String s) {
super(s);
}
}
class Test {
static void blowUp() throws BlewIt {
throw new NullPointerException();
}
public static void main(String[] args) {
Exception e = new NullPointerException();
try {
blowUp();
} catch (BlewIt b) {
System.out.println("Caught BlewIt");
} finally {
System.out.println("Uncaught Exception");
}
}
}
개념을 정리하는 데 도움을주십시오.
'BlewIt'이 'Exception'이라는 이유로 'NullPointerException' 유형이'BlewIt '이 아니라는 뜻입니다. –
@ TobiasBrösamle이 빠른 답장을 보내 주셔서 감사합니다. 당신이 지적했습니다. 다른 것을 시도하고 더 많은 코드를 추가했습니다. – sparsh610
@ TobiasBrösamle BlewIt은 이제 NullPointerException 유형이지만 여전히 동일한 출력을 얻고 있습니다. – sparsh610