自动装箱,异常以及异常处理

1.装箱以及拆箱

1.1包装类

在Java中,包装类(Wrapper Classes)是将基本数据类型封装在一个类中,使它们能够以对象的形式进行操作。Java提供了八种基本数据类型的对应包装类,这些包装类位于java.lang包中。以下是基本数据类型及其对应的包装类:

  1. byte - Byte
  2. short - Short
  3. int - Integer
  4. long - Long
  5. float - Float
  6. double - Double
  7. char - Character
  8. boolean - Boolean

主要功能

  1. 将基本数据类型转换为对象
    包装类使得基本数据类型可以作为对象来处理,可以存储在集合(如ArrayListHashMap)中。

  2. 提供一系列实用方法
    包装类提供了多种方法,用于基本数据类型与字符串之间的转换,以及一些其他的实用操作。

1.2自动装箱及拆箱

int primitiveInt = 100;
Integer boxedInt = primitiveInt;  // 自动装箱:int 转换为 Integer

Integer boxedInt = 100;  // 自动装箱
int primitiveInt = boxedInt;  // 自动拆箱:Integer 转换为 int

2.异常及异常处理

2.1什么是异常

异常是程序在运行是出现的意外情况(错误)。

2.2为什么要处理异常

public class Test {
    public static void main(String[] args) {
        System.out.println("===============计算开始==================");
        int a=10;
        int b=0;
        int c=a/b;
        System.out.println("==============计算的结果:========="+c);
        System.out.println("------------------程序结束---------------");
    }
}

我们可以看到,当程序运行到了int c = a/b;是出现异常并终止了程序运行,那么当我们遇到可能会发生异常的代码时想要执行后面的代码时我们就需要对异常进行处理让程序能够运行下去。

public class Test {
    public static void main(String[] args) {
        System.out.println("===============计算开始==================");
        try{
            int a=10;
            int b=0;
            int c=a/b;
            System.out.println("==============计算的结果:========="+c);
        }catch(Exception e){
            System.out.println("------------------程序结束---------------");
        }
    }
}

我们可以看到,即便程序出现了异常,但是经过处理后程序依然可以运行。

2.3java中处理异常的两种方法try/catch or throws

第一种:try{可能发生异常的代码块}catch(异常类型  对象){捕获异常}

第二种:throws 抛出异常给调用者

2.3.1try/catch

public class Test {
    public static void main(String[] args) {
        System.out.println("===============计算开始==================");
        try{
            int a=10;
            int b=0;
            //因为除数不能为0,所以这里发生了异常,系统自动产生一个Exception(异常)对象
            int c=a/b;
            System.out.println("==============计算的结果:========="+c);

        //在catch中查找上面产生的异常对象,如果匹配,则执行catch内的语句,如果未能找到对应的对象
        // 则将异常交给jvm停止程序
        }catch(Exception e){
            System.out.println("------------------程序结束---------------");
        }
    }
}

在使用中我们也可能会遇到多种异常,这时候我们就可以写多个catch,当使用多个catch的语句时需要注意小范围的异常在上面,大范围的异常则应该在小范围异常的下面。

public class Test {
    public static void main(String[] args) {
        System.out.println("===============计算开始==================");
        Scanner sc=new Scanner(System.in);
        try {
            System.out.println("输入一个数:");
            int a=sc.nextInt();
            System.out.println("输入另一个数:");
            int b=sc.nextInt(); //产生一个InputMismatchException异常对象
            int c = a / b;
            System.out.println("==============计算的结果:=========" + c);
        }catch(ArithmeticException e){
            System.out.println("捕获了算术异常.");
        }catch(InputMismatchException e){
            System.out.println("捕获输入异常");
        }
        System.out.println("------------------程序结束---------------");
    }
}

如果异常的种类比较多,那么我们就可以使用所有异常的父类Throwable,不过一般都可以使用Exception。

Exception类中的常用方法

getMessage() :获取异常的信息。
toString(): 获取异常信息以及异常类型。
printStackTrace() :打印异常信息以及异常类型以及异常发生在哪一行。

2.3.2fianlly关键字

        finally它可以和try catch 配合,也可以单独与try配合。 有没有异常都会被执行的代码。---主要使用在资源得到释放。

package day1121;
/**
 * @author :Zhang
 * @date :Created in 2024/11/22 20:38
 * @description:
 * @modified By:
 * @version:
 */
public class Test5 {
        public static void main(String[] args) {
            show(10,0);
        }
        public static int show(int a,int b)
        {
            try {
                int c=a/b; //ArithmeticException f=new ArithmeticException();
                return c; //return用来结束方法。
            } catch (Exception e) {//Exception e=f;
                System.out.println("异常捕获");
            }finally {
                System.out.println("释放资源"); //IO资源  JDBC资源 连接池资源
            }
            return 0;
        }
    }

2.3.2throws抛出异常

使用方法:

修饰符 返回类型 方法名() throws 异常类型{
   
}

public class Test {
    public static void main(String[] args) throws ArithmeticException {
          show(10,0); //调用者可以处理该异常对象。 也可以继续抛出.
          System.out.println("~~~~~~~~~~~~~~~~~~~");
    }
    public static void show(int a,int b)throws ArithmeticException{
         int c=a/b;
    }
}

异常类型两种:

Exception异常: 编译异常,在编译时必须强制处理,使用try或时直接抛出

RuntimeException异常: 运行时异常,在编译时不要求强制处理

2.4throw关键字

可以通过throw这个关键字,人为创建一个异常对象。

格式:

throw new 异常类型("异常的信息");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值