JAVA 异常

来源:互联网 发布:linux执行sql命令 编辑:程序博客网 时间:2024/05/01 00:23
import java.io.*;
import java.io.IOException;
public class TestException {
public static void main(String[] args)
{
 try
 {
  test();
 }
 catch(FileNotFoundException e)
 {
  System.out.println("Error in FileNotFoundException!");
 }
 catch(IOException e)
 {
  System.out.println("Error in IOException....");
 }
 try
 {
  FileInputStream fileObject = new FileInputStream("myfile.txt");
  int len=fileObject.read();
  System.out.println(len);
  int div=0;
  IOException e=new IOException();
  throw e;//此行之后不能有代码,否则无效,提示出错
  /*(int res=10/div;
  System.out.println(res);*/
  
 }
 catch(IOException e)
 {
  System.out.println("Error in FileInputStream.");
 }
 catch(Exception e)
 {
  System.out.println("Error in div by zero.");
 }
 finally
 {
  System.out.println("Block in Finally...");
 }
 
}
public static void  test()
throws FileNotFoundException,IOException //要在自定义的函数里面抛出异常必须不能少了这行
{
 if (1==1)
 {
  FileNotFoundException e=new FileNotFoundException();
  throw e;
 }
 else
 {
  IOException e2=new IOException();
  throw e2;
 }
 
}
}
 
原创粉丝点击