java异常,异常处理,异常类 关键字:throws 和 throw 自定义的异常类

package cn.kecheng;
import java.util.Scanner;
/**异常:异常是指在程序的运行过程中所发生的不正常的情况,它会中断正在运行的程序
   异常处理机制:java中通过异常处理机制为程序提供异常处理的能力,保持程序继续运行而不中断!
  异常处理:涉及异常处理的关键字有try…catch/try…catch…finally
 * try{有可能产生异常情况的代码段}catch(异常类型){对异常进行处理的代码段}  1.正常执行,没出现任何异常2.出现异常,异常处理,正常结束
 * 把有可能产生异常的代码放到try代码块中,catch代码块负责捕获并处理异常。
 * try{有异常情况的代码段}catch(异常类型){对异常进行处理的代码段}finally{}
 * finally块用于进行收尾工作(关闭数据库、关闭文件、释放内存等资源),不管是否发生异常,finally都执行。finally 总是执行,常用进行收尾类工作。
 * 特殊情况(B)
[1] finally不执行的情况。
System.exit(0) 正常推出(退出)jvm,finally不会执行。
[2]catch可以省略,变成try…finally块。
 */



public class Curriculum {
    public static void main(String[] args) {
        Scanner arr = new Scanner(System.in);
        System.out.println("请输入所选学科:1.语文2.数学3.英语");
        
        try { //我输入a,不符合下面的int的类型,所以出现异常,然后执行直接从下面这行调到cath(异常捕获并处理)那
            int arr1 = arr.nextInt();
            if(arr1==1){
            System.out.println("语文");}
        else if(arr1==2){
            System.out.println("数学");
        }else if(arr1==3){
            System.out.println("英语");
        }else{
            System.out.println("输入有误");
        }
            }//如果不用Exception这个父类,为了进行匹配,可以用多重catch进行多重匹配catch{异常类型1}catch{异常类型2}catch{异常类型3}。。。
         catch (Exception e){//捕获异常,进行异常匹配,【如果异常类型不匹配,程序会中断执行】Exception是所有异常的直接或间接父类
            System.out.println("输入有误");//异常处理
              // 打印异常的信息
              System.out.println(e.toString());
             //打印异常堆栈信息,
             e.printStackTrace();
            //返回异常的描述信息,如果没有信息,返回null(InputMismatchException 没有描述信息)
            System.out.println(e.getMessage());//getMessage:返回异常的描述信息
// jvm 正常结束运行 【System.exit(0)】;如果有这个
finally不会执行。因为代表整个虚拟机都关闭了,其他情况,不管是否有异常,finally都会正常执行
          } finally {
            System.out.println("程序结束");    }  
        //finally块用于进行收尾工作(关闭数据库、关闭文件、释放内存等资源),不管是否发生异常,finally都执行。finally 总是执行,常用进行收尾类工作。
    
        System.out.println("程序结束");
    
    }
}

Throwable 类是 Java 语言中所有错误(Error)或异常(Exception)的父类只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java throw 语句抛出。

Error 表示错误类。仅靠程序本身无法恢复的严重错误。jvm内存耗尽、jvm崩溃等

Exception 类表示异常类可以通过java 异常处理机制处理。

Exception 根据是否处理分为两种情况。

RuntimeException:运行时异常。不要求程序必须做出处理。是所有运行时异常的父类。

CheckedException:检查时异常。要求程序必须处理不处理编译不通过。

package cn.sxt02.exception04;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        // 运行时异常
        Scanner sc = new Scanner(System.in);
        // runtime exception
        int r = sc.nextInt();
        System.out.println("r = "+ r);
        
        // 检查时异常
        SimpleDateFormat df = new SimpleDateFormat();
        try {
            Date date = df.parse("2019");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

常见的运行时异常

ArithmeticException:数学计算异常。比如除数为0

InputMismatchException:输入不匹配异常

ArrayIndexOutofBoundsException:数组下标越界异常。

NullPointException:空指针异常,对象没有初始化就使用时,jvm抛出该异常

IllegalArgumentException:非法参数异常。

ClassCastException:强制类型转换异常。

NumberFormatException:数字格式化异常。比如把“abc”格式化成数字。

常见的检查时异常:

ClassNotFoundException:类没有被发现异常。

SQLException:数据库相关异常

IOException:IO操作异常

ParseException:解析错误异常

FileNotFoundException:文件未发现异常。

 

运行时异常和检查时异常的区别

运行时异常:包括RuntimeException及其所有子类。不要求程序必须对它们作出处理,比如InputMismatchException、ArithmeticException、NullPointerException等。即使没有使用try-catch或throws进行处理,仍旧可以进行编译和运行。如果运行时发生异常,会输出异常的堆栈信息并中止程序执行。

Checked异常(非运行时异常):除了运行时异常外的其他异常类都是Checked异常。程序必须捕获或者声明抛出这种异常,否则出现编译错误,无法通过编译。处理方式包括两种:通过try-catch捕获异常,通过throws声明抛出异常从而交给上一级调用方法处理

package cn.kecheng;
//throws关键字
//当一个方法可能存在异常,而此时自身又无法更好的处理,可以交给外界处理。此时用throws声明并抛出异常。
//开发者可以根据需要声明检查时异常(Exception或者非运行时异常)和运行时异常(RuntimeException或其子类)
//检查时异常:必须处理,否则无法通过编译     运行时异常:可以不处理,没处理也可正常运行
public class Test01 {
    public static int div(int a, int b)throws ArithmeticException{
    //抛出数学计算异常(运行时异常),因为除数可能为0
                int r = a / b;
                return r;
                }

            
        public static void main(String[] args) {

            try {
                Test01.div(10, 0);
            } catch (ArithmeticException e) {
                System.out.println("除数不能为0");
            }
    
    }
}




package cn.kecheng;

public class Test01 {
    public static int div(int a, int b)throws Exception{
    //抛出异常(所有异常的父类,必须解决),因为除数可能为0
                int r = a / b;
                return r;
                }

                                                 //异常上抛
        public static void main(String[] args) throws Exception{
//如果调用处也不知道如何处理异常,可选择继续声明异常,我们把这个过程称为异常上抛。
            //【1】 如果调用处知道如何处理!
            /*
            try {
                Test01.div(10, 0);
            } catch (Exception e) {  //异常捕捉和处理
                e.printStackTrace();//所以返回异常的描述信息,无需上抛
            }
            */
            // 【2】调用处也不知道如何处理,这需要异常上抛
            
                Test01.div(10, 0);
               //不知道如何处理,就直接写调用和异常上抛
    
    }
}   /*声明异常和重载的关系:声明异常和重载没有任何关系
方法重载
[1]方法名相同
[2]参数列表不同(个数、类型、顺序)
[3]和返回值、修饰符、声明异常无关。
*/


 

声明异常和重写的关系

[1]父类方法声明了异常(检测时或运行时),子类可以不声明任何异常。

可以认为:父类方法抛出异常,子类在重写过程把该异常处理掉了所以子类方法不用声明异常

[2] 父类方法声明没有声明任何异常(检测时或运行时),子类不声明异常或者声明运行时异常。

[3] 父类声明了异常(检测时或运行时),子类声明完全一样的异常。

package cn.kecheng;
/**手动抛出异常  关键字:throw
 * 除了系统自动抛出异常外,有些问题需要开发者手动抛出异常。使用关键字throw
 *
 *
 */
public class Student {
    private String name;
    private String gender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }
                                          //声明异常
    public void setGender(String gender) throws Exception{
        if(gender.equals("男") || gender.equals("女")) {  //equals 判断属性值是否相等          
            this.gender = gender;
        }else {
            throw new Exception("性别不合法!");
        }    //手动抛出异常
    }

    public Student(String name, String gender) {
        super();
        this.name = name;
        this.gender = gender;
    }


package cn.kecheng;
import java.util.Scanner;

public class Test01 {


    public static void main(String[] args){
        
        Student stu = new Student();
        stu.setName("二狗");
        try {
            stu.setGender("xxx");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }                  //getMessage:返回异常的描述信息
        
    }
}





package cn.sxt02.exception07;
//自定义的异常类
/*自定义异常
如果开发者需要手动抛出的异常在系统不存在,可以自定义异常。
如果要自定义异常,首先要确定异常类型,如果异常是运行时异常,必须继承RuntimeException或其子类;如果异常是检查时异常,必须继承Exception或其子类。
异常的命名方式,参考系统命名方式,以Exception结尾。*/
public class AgeException extends Exception{

    public AgeException() {
        super();
    }

    public AgeException(String message) {
        super(message);
    }
    
}






package cn.sxt02.exception07;

public class Student {
    private String name;
    private String gender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }
                                               //自定义的异常类
    public void setGender(String gender) throws AgeException{
        if(gender.equals("男") || gender.equals("女")) {            
            this.gender = gender;
        }else {
            throw new AgeException("性别不合法!");
        }         //抛出自定义的异常类
    }

    public Student(String name, String gender) {
        super();
        this.name = name;
        this.gender = gender;
    }

    public Student() {
        super();
    }
    
    

}




package
cn.sxt02.exception07; import java.util.Scanner; public class Test01 { public static void main(String[] args){ Student stu = new Student(); stu.setName("二狗"); try { stu.setGender("xxx"); } catch (AgeException e) { System.out.println(e.getMessage()); } //返回自定义的异常类的信息 } }
原文地址:https://www.cnblogs.com/406070989senlin/p/10776926.html