JAVA 异常

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;
 }
 
}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值