面向对象的梳理(7)关于异常

本文介绍了Java中的异常及其处理机制,包括数学异常、输入类型不匹配异常、空指针异常、数组下标越界异常和类型转换异常的示例,并展示了使用try-catch-finally、throw和throws处理异常的方法。
摘要由CSDN通过智能技术生成

目录

1.前言

2.什么是异常?

3.什么是异常处理?

4.常见的异常类型

(1)数学异常(AirthmeticException)

(2)输入类型不匹配异常(InputMismatchException)

(3)空指针异常(NullPointerException)

(4)数组下标越界异常(ArrayIndexOutOfBoundsException)

(5)类型转换异常(ClassCastException)

5.如何处理异常?

(1)try  catch   finally

(2)throw

(3)throws

6.结尾


1.前言

在写异常前,我们先来思考一个问题——什么是异常呢?

在生活中我们又会遇到哪些异常?比如被除数为0?根号下是负数?吃饭没有筷子?没错,这些都可以算作异常,而在Java中的异常与这些异常可以说是大同小异,下面让我们来深入的学习面向对象的最终章——异常章节吧。

2.什么是异常?

异常是指在程序运行过程中所发生的不正常的事件,他会中断正在运行的程序。

3.什么是异常处理?

在Java中,异常处理是一种机制,用于在程序运行时捕获和处理出现的错误或异常。当程序运行时遇到错误或异常时,它会抛出一个异常。异常处理机制允许程序员捕获这些异常并根据需要执行适当的操作,例如记录错误信息,重试操作,或者向用户提供有用的提示信息。在Java中,异常处理由try-catch块和throw语句组成。

4.常见的异常类型

(1)数学异常(AirthmeticException)

int b=10;
int c=0;
System.out.println(b/c);

(2)输入类型不匹配异常(InputMismatchException)

Scanner sc=new Scanner(System.in);
int b=10;
System.out.println("请输入除数:");
int c=sc.nextInt();
//当输入的数据类型不是int类型时,就是输入类型不匹配异常。
System.out.println(b/c);

(3)空指针异常(NullPointerException)

一般字符串为空再调用isEmpty()就会出现空指针异常,这个时候就需要我们去限制字符串非空等条件了。

  String m=null;
  if (m.isEmpty()){
     System.out.println(m);
     }

(4)数组下标越界异常(ArrayIndexOutOfBoundsException)

int arr[]=new int[2];
arr[1]=10;
arr[2]=12;
arr[0]=15;

(5)类型转换异常(ClassCastException)

 String string=new String("good");
 Object object=string;
 int a=(int) object;

5.如何处理异常?

(1)try  catch   finally

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryCatch {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        try {
            System.out.println("请输入被除数:");
            int a=scanner.nextInt();
            System.out.println("请输入除数");
            int b=scanner.nextInt();
            System.out.println("商是"+a/b);
            return;
        }catch (ArithmeticException a){
            System.out.println("数学异常");
            String message=a.getMessage();
            System.out.println(message);
        }catch (InputMismatchException i){
            System.out.println("数据类型输入异常");
            String message=i.getMessage();
            System.out.println(message);
        }finally {
            System.out.println("程序结束");
        }
    }
}

(2)throw

public class TestA {
    public static void main(String[] args) {
        Student student=new Student();
        student.setSex("10");
        student.show();
    }
    static class Student{
        public Student() {
        }

        public Student(String sex) {
            this.sex = sex;
        }

        private String sex;

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            if (sex=="男"||sex=="女"){
                this.sex = sex;
            }else {
                try {
                    throw new MyException("空指针异常");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public void show(){
            System.out.println("性别是"+sex);
        }
}

}

(3)throws

public class TestB{
    public static void main(String[] args) throws Exception{
        int fun=TestC.fun(10,0);
        System.out.println(fun);
    }
    public static int fun(int a,int b) {
        int result=a/b;
        return result;
    }
}
public class TestC {
    public static void main(String[] args) {
        try {
            int m=TestA.fun(4,0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static int fun(int a,int b) throws Exception{
       int result=a/b;
       return result;
    }
}

6.结尾

关于异常的知识点大概这么多,面向对象的章节到此就告一段落了,如有不足欢迎指出。

下面将进行集合、实用类等知识点的书写。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值