JAVA自学笔记(6)—异常、线程、函数式编程

转自:头条作者:java一线架构师1.异常1.0 异常的概念2.throw关键字publicclassMain{publicstaticvoidmain(String[] args) {int[]arc=null;getelem(arc,0);}privatestaticintgetelem(intarc[],intindex) {if(arc==null){...
摘要由CSDN通过智能技术生成

转自:头条

作者:java一线架构师

1.异常

1.0 异常的概念

 2.throw关键字

public class Main{
    public static void main(String[] args) {
        int []arc=null;
        getelem(arc,0);
    }
 
    private static int getelem(int arc[],int index) {
        if(arc==null){
            throw new NullPointerException("空指针异常!");
        }
        return arc[index];
    }
 
}

public class Main{
    public static void main(String[] args) {
        int []arc=new int[3];
        getelem(arc,3);
    }
 
    private static int getelem(int arc[],int index) {
        if(arc==null){
            throw new NullPointerException("空指针异常!");
        }
        else if(index>=arc.length||index<0)
        {
            throw new ArrayIndexOutOfBoundsException("下标超出数组的范围!");
        }
        return arc[index];
    }
 
} 

3.Objects 非空判断

import java.util.Objects;
 
public class Main{
    public static void main(String[] args) {
        int arc[]=null;
        Objects.requireNonNull(arc,"空指针异常");
    }
}
  

4.异常处理的第一种方法 throws关键字

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.module.FindException;
 
public class Main {
    public static void main(String[] args) throws Exception {
        //判断路径是否是C:\\.txt
        Filename("C:\\.tx");
    }
 
    private static void Filename(String s) throws Exception {
        //FileNotFoundException extends IOException extends Exception
        if(!s.equals("C:\\.txt")){
            throw new FileNotFoundException("该路径不是C:\\.txt");
        }
         if(!s.endsWith(".txt"))
             throw new IOException("文件后缀不是.txt");
 
    }
}

5.异常处理的第二种方法 

try...catch()

import java.lang.invoke.MethodHandles;
 
public class Main{
    public static void main(String[] args) {
        int[] arc=new int[3];
        try {
            int getelem = getelem(arc, 3);
        }catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("程序由catch处理");
        }
        System.out.println("后续代码");
        //如果抛出throws 程序交给JVM处理 程序遇到异常就会中断
 
 
    }
 
    private static int getelem(int arc[],int index) throws ArrayIndexOutOfBoundsException{
        if(index<0||index>=arc.length)
        {
            throw new ArrayIndexOutOfBoundsException("下标超出数组的长度范围");
        }
        return arc[index];
    }
 
 
}

打印结果:

程序由catch处理后续代码

6.Throwable类中常用的方法

import java.lang.invoke.MethodHandles;
 
public class Main{
    public static void main(String[] args) {
        int[] arc=new int[3];
        try {
            int getelem = getelem(arc, 3);
        }catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e.getMessage());
            System.out.println(e.toString());
            System.out.println(e);
            e.printStackTrace();
        }
        System.out.println("后续代码");
        //如果抛出throws 程序交给JVM处理 程序遇到异常就会中断
 
 
    }
 
    private static int getelem(int arc[],int index) throws ArrayIndexOutOfBoundsException{
        if(index<0||index>=arc.length)
        {
            throw new ArrayIndexOutOfBoundsException("下标超出数组的长度范围");
        }
        return arc[index];
    }
 
 
}

  打印结果:

下标超出数组的长度范围java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围后续代码java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围at Main.getelem(Main.java:24)at Main.main(Main.java:7)

 7.异常处理的注意事项

(1)多个异常对象

import java.util.List;
/*
* 异常处理:多个异常对象的处理
* 1、多个异常分别处理
* 2、多个异常一次捕获,多次处理
* 3、多个异常一次捕获一次处理
* */
 
public class Main{
    public static void main(String[] args) {
        //int []arc=new int[3];
        //System.out.println(arc[3]);//ArrayIndexOutOfBoundsException 3
        //List<Integer> list = List.of(4, 3, 26, 6);
        //System.out.println(list.get(4));//IndexOutOfBoundsException
 
        /*1.多个异常分别处理
        try{
            int []arc=new int[3];
            System.out.println(arc[3]);
        }catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
 
        try{
            List<Integer> list = List.of(4, 3, 26, 6);
            System.out.println(list.get(4));
        }catch(IndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
 
        java.lang.ArrayIndexOutOfBoundsException: 3
        java.lang.IndexOutOfBoundsException: Index 4 out-of-bounds for length 4
 
      */
 
        /*

        2.多个异常一次捕获多次处理

        注意事项 子类对象必须写在父类对象之上

        try{
            int []arc=new int[3];
            System.out.println(arc[3]);
            List<Integer> list = List.of(4, 3, 26, 6);
            System.out.println(list.get(4));
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }
        catch (IndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
    }
      */
 
        /*

        3、多个异常一次捕获一次处理

       try{
            int []arc=new int[3];
            System.out.println(arc[3]);
            List<Integer> list = List.of(4, 3, 26, 6);
            System.out.println(list.get(4));
        }
         catch(Exception e)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值