标签:
示例程序:
public class TestThrows { public static void main(String[] args) { FileInputStream fis = new FileInputStream("a.txt"); } }
为什么以上程序完全符合Java语法规范,但是却会报错呢?
修正:
public class TestThrows
{
public static void main(String[] args)
throws FileNotFoundException
{
FileInputStream fis = new
FileInputStream("a.txt");
}
}
import java.io.*; public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉异常"); } } private static void throwsTest() throws ArithmeticException,IOException { System.out.println("这只是一个测试"); // 程序处理过程假设发生异常 throw new IOException(); //throw new ArithmeticException(); } }
结果截图:

import java.io.*;
public class OverrideThrows
{
public void test()throws IOException
{
FileInputStream fis = new FileInputStream("a.txt");
}
}
class Sub extends OverrideThrows
{
//如果test方法声明抛出了比父类方法更大的异常,比如Exception
//则代码将无法编译……
public void test() throws FileNotFoundException
{
//...
}
}
try { //... throw new SocketException(); } catch (SocketException | SecurityException | NullPointerException e) { //exception handler }
标签:
原文地址:http://www.cnblogs.com/389629916muyachao/p/4965043.html