程序的异常

异常:程序在运行过程中出现的不正常情况
代码写错了,不属于异常

出现异常,会导致jvm(虚拟机)停止运行,后续程序无法运行

java中默认的异常处理机制:将出现的异常按不同的类型分类,为每一种异常封装了一个类来进行表示。出现某种异常情况时,会抛出此类的对象,然后终止虚拟机的运行

使用异常处理机制,对程序运行过程中出现的异常情况进行捕捉并处理异常:程序在运行过程中出现的不正常情况
代码写错了,不属于异常

//算术异常

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


//数组越界异常

int[] c=new int[2];
c[3]=0;


//字符串索引越界异常

String s="abc";
s.charAt(4);


//类型转化异常

Object s=new Integer(10);
String s1=(String)s;


//数字格式化异常

Integer.parseInt("abc");

//空指针异常

String s=null;
s.lenght;


package Day1;

public class Demo1 {
    public static void main(String[] args) {
        //算术异常
        int a=10;
        int b=0;
        System.out.println(a/b);//java.lang.ArithmeticException
        //数组越界异常
        int[] s=new int[2];
        s[3]=0;//java.lang.ArrayIndexOutOfBoundsException
        //字符串索引越界异常
        String n="abc";
        n.charAt(4);//java.lang.StringIndexOutOfBoundsException
        //类型转换异常
        Object m=new Integer("abc");
        String s1=(String)m;//java.lang.NumberFormatException
        //数字格式化异常
        Integer.parseInt("abc");//java.lang.NumberFormatException
        //空指针异常
        String c=null;
        c.length();//java.lang.NullPointerException
    }
}

java.lang.Throwable

异常体系中的超类

Error错误

是虚拟机和java代码无法解决的问题,例如虚拟机内存不够用或内部错误

StackOverflowerError

栈溢出错误,栈空间不够用

OutOfMemoryError

堆溢出错误,堆空间不够用

Exception异常

这类异常是可以通过异常处理机制进行处理的一般异常

1.运行期异常:

在编译期间,编译器不会主动的提示程序员进行异常处理

2.编译器异常:

在编译期间,编译器会主动的提示程序员进行异常处理

两者的区别在于异常类有没有继承RuntimeException

异常处理

在编码时,就针对可能出现的问题预习编写一些处理机制

程序运行

出现异常

执行处理机制

继续运行后续程序

try{
    //编写可能出现异常的代码
}catch(//异常类型){
    //处理机制
    }
    
    
    public class Demo2 {
    public static void main(String[] args) {
        int a=10;
        int b=0;
        try{
            System.out.println(a/b);
        }catch (ArithmeticException s){
            s.printStackTrace();
            System.out.println("错误");
        }
        System.out.println("朱师磊暗区出不了机密");
    }
}
catch

一个try可以对应多个catch

package Day1;

public class Demo2 {
    public static void main(String[] args) {
        int a=10;
        int b=0;
        try{
            int c=Integer.parseInt("abc");
            System.out.println(a/b);
        }catch (ArithmeticException s){
            s.printStackTrace();
            System.out.println("错误");
        }catch (ArrayIndexOutOfBoundsException m){
            System.out.println("!!!!");
        }catch (Exception e){
            e.printStackTrace();
            e.getMessage();
            System.out.println("系统忙,请稍后再试");
        }
        System.out.println("朱师磊暗区出不了机密");
    }
}
//finally

package Day1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = null;
        try{
            inputStream=new FileInputStream("D:/demo.txt");
            while(inputStream.read()!=-1){

            }
        }catch (FileNotFoundException s){
            s.printStackTrace();
            s.getMessage();
            System.out.println("文件找不到异常");
        } finally {
            if(inputStream!=null){
                inputStream.close();
            }
        }
        System.out.println("苟浩亮暗区出不了机密");
    }
}

throws关键字

用在方法声明部分,声明方法要抛出的各种异常。表明此方法中可能会出现的异常,并且该方法不处理异常,谁调用谁处理

package Day2;

public class Demo1 {
    public static void main(String[] args) {
        try{
            A();
        }catch (NumberFormatException s){
            s.printStackTrace();
            System.out.println("错误");
        }
        System.out.println("朱师磊暗区出不了机密");
    }
    public  static  void A() throws  NumberFormatException{
        B();
    }
    public  static  void  B() throws  NumberFormatException{
        int c=Integer.parseInt("abc");
    }
}
throw关键字

在方法体中,用于显示抛出异常,抛出一个具体的异常对象,该方法终止运行

在异常对象的构造方法中,声明异常原因

package Day2;

public class Demo2 {
    public static void main(String[] args) {
        try {
            text(101);
        } catch (SoreException s) {
            s.printStackTrace();
            s.getMessage();
        }
    }

    public static void text(int a) throws SoreException {
        if (a < 0 || a > 100) {
            throw new SoreException("分数异常");
        } else {
            System.out.println("你的分数为"+a);
        }
    }

    public static class SoreException extends Exception {
        public SoreException(String message) {
            super(message);
        }
    }
}
自定义异常

根据自己的需求,自定义一个异常类,与java中的异常类进行区分

    public static class SoreException extends Exception {
        public SoreException(String message) {
            super(message);
        }
    }
}

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值