Java未报告异常 [英] Java unreported exception

查看:204
本文介绍了Java未报告异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习Java的同时,我经常绊倒这个错误。它就像这样:


未报告的异常java.io.FileNotFound异常;必须被捕获或声明被抛出。


java.io.FileNotFound只是一个例子,我看过许多不同的。在这种特殊情况下,导致错误的代码是:

  OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(myfile PDF))); 

错误总是消失,代码编译和一旦我将该语句放在try / catch块中,就会成功运行。有时它对我来说足够好,但有时不是。



首先,我正在学习的示例并不总是使用try / catch,但是显然应该工作。



更重要的是,有时候当我将整个代码放在try / catch中时,它根本无法工作。例如。在这种特殊情况下,我需要在 finally {} 块中 out.close(); 但是如果上面的声明在 try {} 之内,则终于{} 不会看到 out ,因此无法关闭它。 p>

我的第一个想法是导入java.io.FileNotFound; 或其他相关的异常,但没有帮助。


你所指的是检查异常,这意味着它们必须被声明或处理。处理Java文件的标准结构如下所示:

  InputStream in = null; 
尝试{
in = new InputStream(...);
//做东西
} catch(IOException e){
//最后执行
} {
if(in!= null){
尝试{
in.close();
} catch(Exception e){
}
}
}

是丑吗?当然。是冗长的吗当然。 Java 7将使它更好的ARM块,但直到那时你被困在上面。



你也可以让调用者处理异常:

  public void doStuff()throws IOException {
InputStream in = new InputStream(...);
//做东西
in.close();
}

虽然即使是 close()应该可能被包裹在一个最终块中。



但是上面的函数声明说这个方法可以抛出一个 IOException 。因为这是一个被检查的例外,这个函数的调用者将需要 catch 它(或声明它,所以它的调用者可以处理它等等)。


While learning Java I stumble upon this error quite often. It goes like this:

Unreported exception java.io.FileNotFound exception; must be caught or declared to be thrown.

java.io.FileNotFound is just an example, I've seen many different ones. In this particular case, code causing the error is:

OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("myfile.pdf")));

Error always disappears and code compiles & runs successfully once I put the statement inside try/catch block. Sometimes it's good enough for me, but sometimes not.

First, examples I'm learning from do not always use try/catch and should work nevertheless, apparently.

Whats more important, sometimes when I put whole code inside try/catch it cannot work at all. E.g. in this particular case I need to out.close(); in finally{ } block; but if the statement above itself is inside the try{ }, finally{} doesnt "see" out and thus cannot close it.

My first idea was to import java.io.FileNotFound; or another relevant exception, but it didnt help.

解决方案

What you're referring to are checked exceptions, meaning they must be declared or handled. The standard construct for dealing with files in Java looks something like this:

InputStream in = null;
try {
  in = new InputStream(...);
  // do stuff
} catch (IOException e) {
  // do whatever
} finally {
  if (in != null) {
    try {
      in.close();
    } catch (Exception e) {
    }
  }
}

Is it ugly? Sure. Is it verbose? Sure. Java 7 will make it a little better with ARM blocks but until then you're stuck with the above.

You can also let the caller handle exceptions:

public void doStuff() throws IOException {
  InputStream in = new InputStream(...);
  // do stuff
  in.close();
}

although even then the close() should probably be wrapped in a finally block.

But the above function declaration says that this method can throw an IOException. Since that's a checked exception the caller of this function will need to catch it (or declare it so its caller can deal with it and so on).

这篇关于Java未报告异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆