系统常用标准异常类 Java

题目详情:

常用异常类NumberFormatException,IndexOutOfBoundsException、ArithmeticException等,本程序,建立一个Test_Exception类,里面有一个test方法,用于检测以上异常对象的发生并输出相应信息。 比如发生NumberFormatException异常,输出"数据格式异常";发生IndexOutOfBoundsException异常,输出"越界异常";发生ArithmeticException异常,输出"算术运算异常"。 ##。# Test_Exception类定义:

class Test_Exception{
    void test() {
        int n = 0,m = 0,t = 1000;
        int[] a=new int[4];
        int[] b=null;
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        try {
                    m = Integer.parseInt(str);
                    t=t/m;
                    a[m]=m;
                    b[0]=m;
        }
        //catch部分需要你们自己填写
        catch(...){...}
        catch(...){...}
        catch(...){...}
        catch(Exception e) {
          System.out.println("Exception:"+e.getMessage()); 
         }  //先catch子类Exception,后catch父类
}
}

前面3个catch 需要你 编写,发生对应异常,输出相应信息。

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Test_Exception te=new Test_Exception();
        te.test();
    }
}
/* 请在这里编写完整 Test_Exception类 */

输入样例:

0

结尾无空行

输出样例:

算术运算异常

答案代码: 



class Test_Exception
{
    void test() 
    {
        int n = 0,m = 0,t = 1000;
        int[] a=new int[4];
        int[] b=null;
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        try 
        {
            m = Integer.parseInt(str);
            t=t/m;
            a[m]=m;
            b[0]=m;
        }
        //catch部分需要你们自己填写
        catch(NumberFormatException e){
            System.out.println("数据格式异常");
        }
		catch(IndexOutOfBoundsException e){
            System.out.println("越界异常");
        }
		catch(ArithmeticException e){
            System.out.println("算术运算异常");
        }
		catch(Exception e) {
            System.out.println("Exception:"+e.getMessage());
        }  //先catch子类Exception,后catch父类
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值