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

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

问题描述

在学习 Java 时,我经常偶然发现这个错误.它是这样的:

<块引用>

未报告的异常 java.io.FileNotFound 异常;必须被抓住或宣布被扔掉.

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

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

错误总是消失并且代码编译 &一旦我将语句放入 try/catch 块中,就会成功运行.有时它对我来说已经足够了,但有时却不行.

首先,我从中学习的示例并不总是使用 try/catch,但显然应该可以工作.

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

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

解决方案

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

InputStream in = null;尝试 {in = new InputStream(...);//做东西} 捕捉(IOException e){//做任何事} 最后 {如果(在!= null){尝试 {附寄();} 捕获(异常 e){}}}

丑吗?当然.它很冗长吗?当然.Java 7 将在 ARM 块上做得更好,但在那之前你会被上面的东西卡住.

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

public void doStuff() 抛出 IOException {InputStream in = new InputStream(...);//做东西附寄();}

尽管如此,close() 也应该被包装在 finally 块中.

但是上面的函数声明说这个方法可以抛出一个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天全站免登陆