Penseur de marche 2020-05-16 17:24 采纳率: 50%
浏览 230
已采纳

请问这些类怎么用?(NullPointerException、ClassCastException、NegativeArraySizeException、ArrayIndexOutofBoundsException)?

这个题目是什么意思呢?
编写一个程序,在main方法中,输入语句,根据不同的错误捕获相应的异常。(NullPointerException、ClassCastException、NegativeArraySizeException、ArrayIndexOutofBoundsException)

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-05-16 20:43
    关注
    import java.*;
    import java.lang.*;
    import java.util.*;
    class test  
    {
    public static void main (String[] args) throws NullPointerException,ClassCastException,NegativeArraySizeException,ArrayIndexOutOfBoundsException
    {
    try
    {
    Integer i = null;
    System.out.println(i.toString());
    }
    catch (NullPointerException ex1)
    {
    System.out.println("NullPointerException");
    }
    
    try
    {
    Object i2 = 123;
    int[] arr = (int[])i2;
    }
    catch (ClassCastException ex2)
    {
    System.out.println("ClassCastException");
    }
    
    try
    {
    int[] arr2 = new int[-1];
    }
    catch (NegativeArraySizeException ex3)
    {
    System.out.println("NegativeArraySizeException");
    }
    
    try
    {
    int[] arr3 = new int[10];
    System.out.println(arr3[11]);
    }
    catch (ArrayIndexOutOfBoundsException ex4)
    {
    System.out.println("ArrayIndexOutOfBoundsException");
    }
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误