2 sixping sixping 于 2016.03.31 00:01 提问

按书上写了一段代码java异常处理,不知道为什么不能运行,求大神指教

源代码如下:
package com.Sixping.Ncre;

import java.io.*;

public class ExceptionCatch {
public static void main(String[] args) {
try {
FileNotFoundException fis = new FileNotFoundException("text");
System.out.println("content of text is:");
} catch (FileNotFoundException e) {
System.out.println(e);
System.out.println("massage:" + e.getMessage());
e.printStackTrace(System.out);
} catch (IOException e) {
System.out.println(e);
}
}
}
运行显示如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body
Unreachable catch block for IOException. This exception is never thrown from the try statement body

at com.Sixping.Ncre.ExceptionCatch.main(ExceptionCatch.java:10)

求大神指教。

5个回答

qq_25827845
qq_25827845   2016.03.31 09:38

这段代码编译过不去,很明显说了,需要throws IOException,至于为什么try-catch捕获不了,坐等大神讲解

caozhy
caozhy   Ds   Rxr 2016.03.31 00:14

说的很清楚
This exception is never thrown from the try statement body
这个异常在你的try语句体永远不会被丢出
FileNotFoundException fis = new FileNotFoundException("text");
throw fis; //加上

sl_18500
sl_18500   2016.03.31 12:27

需要抛出异常撒

sixping
sixping   2016.03.31 17:17

package com.Sixping.Ncre;

import java.io.*;

public class ExceptionCatch {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("text");
System.out.println("content of text is:");
} catch (FileNotFoundException e) {
System.out.println(e);
System.out.println("massage:" + e.getMessage());
e.printStackTrace(System.out);
} catch (IOException e) {
System.out.println(e);
}
}
}

对不起,是我抄错了。

不过诸位大神说的值得研究。
flueky
flueky   2016.03.31 00:08

throw new filenotfoundexception⋯⋯

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!