zoukankan      html  css  js  c++  java
  • 抛出异常(新手)

    这个目前还是处于懵逼状态,只是把写法写出来,明天老师还会细讲。

    ==================================================

    public class yichanglx {
    public static void main(String[] args) throws Exception {
    //抛出异常你可以理解为在你做此项目的时候,你感觉这个地方有可能会出现异常,
    //可以写上抛出异常,到最后可以给你自己一个提示,也可以给别人一个提示。
    int arr[] = {0,2};
    getArray(arr);
    }

    private static int getArray(int[] arr)throws Exception {
    if (arr.length==0){
    throw new Exception("");

    }
    return 1;
    }
    }
    =======================================================
    public class zylx2 {
    public static void main(String[] args){
    int[] arr = null;
    try{
    int i = getArray(arr);
    }catch (Exception e ){
    System.out.println(e);
    }
    }

    private static int getArray(int[] arr) throws Exception {
    if (arr == null){
    throw new NullPointerException("数组不存在。");
    }if (arr.length<3){
    throw new ArrayIndexOutOfBoundsException("数组没有3这个索引");
    }
    return arr[3]+1;
    }
    }
  • 相关阅读:
    「题解」洛谷 P1731 [NOI1999]生日蛋糕
    「题解」洛谷 P1063 能量项链
    Log4j2笔记
    基数排序
    会计知识
    归并排序
    CF668 题解
    拉格朗日反演
    [国家集训队]数颜色 / 维护队列 「带修莫队」
    简单的填数「贪心」
  • 原文地址:https://www.cnblogs.com/lxr521/p/10581198.html
Copyright © 2011-2022 走看看