파일을 읽고 쓸 때 발생하는 모든 예외를 어떻게 잡을 수 있습니까?
Java exceptions
에서 예외를 개별적으로 catch하는 대신 모두를 가져 오는 (catch) 방법이 있습니까?
원하는 경우 메서드에 throws 절을 추가 할 수 있습니다. 그러면 확인 된 메서드를 바로 잡을 필요가 없습니다. 이렇게하면 exceptions
나중에 (아마도 other와 동시에) 잡을 수 있습니다 exceptions
.
코드는 다음과 같습니다.
public void someMethode() throws SomeCheckedException {
// code
}
그런 다음 나중에 exceptions
해당 방법으로 처리하고 싶지 않은 경우 처리 할 수 있습니다 .
모든 예외를 포착하기 위해 일부 코드 블록이 던질 수 있습니다. (이것은 또한 Exceptions
직접 작성한 것을 포착 합니다)
try {
// exceptional block of code ...
// ...
} catch (Exception e){
// Deal with e as you please.
//e may be any type of exception at all.
}
작동하는 이유는 Exception
모든 예외의 기본 클래스 이기 때문 입니다. 따라서 throw 될 수있는 모든 예외는 Exception
(대문자 'E')입니다.
자체 예외를 처리하려면 먼저 catch
일반 예외 앞에 블록을 추가하면 됩니다.
try{
}catch(MyOwnException me){
}catch(Exception e){
}
원시 예외를 포착하는 것이 좋은 스타일이 아니라는 데 동의하지만, 우수한 로깅을 제공하는 예외를 처리하는 방법과 예상치 못한 문제를 처리하는 기능이 있습니다. 예외적 인 상태이기 때문에 응답 시간보다 좋은 정보를 얻는 데 더 관심이있을 수 있으므로 성능 인스턴스가 큰 타격을 입어서는 안됩니다.
try{
// IO code
} catch (Exception e){
if(e instanceof IOException){
// handle this exception type
} else if (e instanceof AnotherExceptionType){
//handle this one
} else {
// We didn't expect this one. What could it be? Let's log it, and let it bubble up the hierarchy.
throw e;
}
}
그러나 이것은 IO가 오류를 발생시킬 수 있다는 사실을 고려하지 않습니다. 오류는 예외가 아닙니다. 둘 다 기본 클래스 Throwable을 공유하지만 오류는 예외와 다른 상속 계층 아래에 있습니다. IO가 오류를 던질 수 있으므로 Throwable을 잡을 수 있습니다.
try{
// IO code
} catch (Throwable t){
if(t instanceof Exception){
if(t instanceof IOException){
// handle this exception type
} else if (t instanceof AnotherExceptionType){
//handle this one
} else {
// We didn't expect this Exception. What could it be? Let's log it, and let it bubble up the hierarchy.
}
} else if (t instanceof Error){
if(t instanceof IOError){
// handle this Error
} else if (t instanceof AnotherError){
//handle different Error
} else {
// We didn't expect this Error. What could it be? Let's log it, and let it bubble up the hierarchy.
}
} else {
// This should never be reached, unless you have subclassed Throwable for your own purposes.
throw t;
}
}
기본 예외 '예외'포착
try {
//some code
} catch (Exception e) {
//catches exception and all subclasses
}
It is bad practice to catch Exception -- it's just too broad, and you may miss something like a NullPointerException in your own code.
For most file operations, IOException is the root exception. Better to catch that, instead.
Yes there is.
try
{
//Read/write file
}catch(Exception ex)
{
//catches all exceptions extended from Exception (which is everything)
}
You may catch multiple exceptions in single catch block.
try{
// somecode throwing multiple exceptions;
} catch (Exception1 | Exception2 | Exception3 exception){
// handle exception.
}
Do you mean catch an Exception
of any type that is thrown, as opposed to just specific Exceptions?
If so:
try {
//...file IO...
} catch(Exception e) {
//...do stuff with e, such as check its type or log it...
}
'programing tip' 카테고리의 다른 글
소켓 홈브류를 통해 로컬 MySQL 서버에 연결할 수 없습니다. (0) | 2020.08.28 |
---|---|
ASP.NET 웹 페이지를로드 할 때 Underscore.js에 대한 Javascript ".map"파일이 누락 됨 (0) | 2020.08.28 |
Java의 모듈러스가 음수로 작동하도록하는 가장 좋은 방법은 무엇입니까? (0) | 2020.08.28 |
bash 쉘 스크립트에 파일을 포함하는 방법 (0) | 2020.08.28 |
parallel.foreach를 중단 하시겠습니까? (0) | 2020.08.28 |