未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出 [英] unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

查看:2256
本文介绍了未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个类 - 只是一个类,没有main(),并且我收到此错误的未运行的异常java.io.FileNotFoundException;必须被捕获或声明为抛出:

I am creating a class -- just a class, no main() and I am receiving the error of "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" at this line:

FileOutputStream outStr = new FileOutputStream(FILE, true);   

我不明白;我尝试{} catch {}块,并且仍然报告错误。

I don't understand; I put in a try{} catch{} block and it's still reporting the error.

此外,它还报告了一个非法开始类型的尝试和两者抓住线条,并且还表示对于两条捕捉线都是;。

Additionally, it's also reporting an "illegal start of type" for the try and both catch lines, and it's also saying that ';' is expected for both catch lines.

我正在使用NetBean IDE,FYI。

I'm using the NetBean IDE, FYI.

感谢您的帮助。

以下是完整的代码:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileNotFoundException;


public class UseLoggingOutputStream 
{

    String FILE = "c:\\system.txt";

    try
    {

        FileOutputStream outStr = new FileOutputStream(FILE, true);

    }

    catch(FileNotFoundException fnfe)
    {

        System.out.println(fnfe.getMessage());

    }

    catch(IOException ioe)
    {

        System.out.println(ioe.getMessage());

    }

}


推荐答案

您需要将文件处理语句放在一个方法中:

You need to put the file processing statements inside a method:

import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class UseLoggingOutputStream {
    public void myMethod() {
        String file = "c:\\system.txt";
        try {
            FileOutputStream outStr = new FileOutputStream(file, true);
        } catch(FileNotFoundException fnfe) { 
            System.out.println(fnfe.getMessage());
        } 
    }
}

这篇关于未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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