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 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答