-4
을 FileWriter 예외를 처리하는 방법을 몰라, 내 방법 중 하나는 말한다 FileWriter의 경우.내 코드에서
예외를 수정하기 위해 try 및 catch 문에 무엇을 넣어야합니까?
을 FileWriter 예외를 처리하는 방법을 몰라, 내 방법 중 하나는 말한다 FileWriter의 경우.내 코드에서
예외를 수정하기 위해 try 및 catch 문에 무엇을 넣어야합니까?
모든 종류의 예외를 처리하는 방법은 Java 개발에 필수적입니다. 을 할 수있는 두 가지 방법이 있습니다 :
public void write(String text) //notice I deleted the throw
{
try{
FileWriter writer = new FileWriter(path, true);
PrintWriter printer = new PrintWriter(writer);
printer.printf("%s" + "%n", text);
printer.close();
catch(IOException ioe){
//you write here code if an ioexcepion happens. You can leave it empty if you want
}
}
와 ...는
public void write(String text) throws IOException //See here it says throws IOException. You must then handle the exception when calling the method
{
FileWriter writer = new FileWriter(path, true);
PrintWriter printer = new PrintWriter(writer);
printer.printf("%s" + "%n", text);
printer.close();
}
//like this:
public static void main(String[] args) //or wherever you are calling write from
{
try{
write("hello"); //this call can throw an exception which must be caught somewhere
}catch(IOException ioe){/*whatever*/}
}
당신은, try 블록에 this.write``에 전화를 넣어이 오류에 언급 된 예외를 catch해야
메시지를 작성한 다음 적절하고 적절하게 처리하십시오. 하지만 자바 프로그래머를 안다면 아마도'printStackTrace' 호출을하고 나머지는 잊어 버릴 것입니다. –
이 메서드는 예외를 throw하지만 try/catch 블록을 사용하여 예외를 catch하지 않는다고 말합니다. – Li357
예외를 처리해야합니다. [예외 잡기 및 처리하기] (https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html)를 참조하십시오. – copeg