【Java】异常处理与try...catch语句

知识点

如果try捕获到异常,则会与和它匹配的catch中的异常类型依次进行比对,如果相同则进行处理;如果不匹配,则继续匹配后续的catch类型;如果都不匹配,那么表示该异常无法由catch块处理,需要采用JVM默认的异常处理方式进行处理。最后不管有没有异常,都会执行finally中的语句块

当使用的时候,try 语句是必须的;catch(err) 里面的参数是必须的; catch 和 finally 都是可选的。

三种形式

try...catch
try...finally
try...catch..finally

关于return的问题

若把 return 写入到了函数的 finally 里面,则最终函数(整个try/catch/finally)的返回值(或者抛出的异常)将是 finally 里面返回的值,即使前面 try/catch 出现了 retrun

throw 关键字

throw 语句与异常类型一起使用。 Java 中有许多异常类型可用: ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc;

throw 语句用来直接拋出一个异常,后接一个可拋出的异常类对象,其语法格式如下:

throw ExceptionObject;

其中,ExceptionObject 必须是 Throwable 类或其子类的对象

如果是自定义异常类,也必须是 Throwable 的直接或间接子类。例如,以下语句在编译时将会产生语法错误:

throw new String("拋出异常");    // 因为String类不是Throwable类的子类
//实例
//如果年龄低于 18 岁,则抛出异常(打印"访问被拒绝")。如果年龄为 18 岁或以上,请打印"访问权限":

public class MyClass {
  static void checkAge(int age) {
    if (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
    }
    else {
      System.out.println("Access granted - You are old enough!");
    }
  }

  public static void main(String[] args) {
    checkAge(15); // 将年龄设置为 15 岁(低于 18 岁......)
  }
}

throws 关键字

通常在一个方法(类)的声明处通过 throws 声明方法(类)可能拋出的异常信息,而在方法(类)内部通过 throw 声明一个具体的异常信息。

throw用在方法实现中,而throws用在方法声明中;
throw只能用于抛出一种异常,而throws可以抛出多个异常

public class Example {
 
    public static void main(String[] args) {
        
      try {
         int result = divide(4,2);
          System.out.println(result);
       } catch (Exception e) {
        e.printStackTrace();
       }
      
    }
    
    public static int divide(int x,int y) throws Exception
    {
        int result = x/y;
        return result;
    }
 
}

例子

package org.example;
//类声明,声明一个Exception的子类NoLowerLetter
class NoLowerLetter extends Exception {
    public void print() {
        System.out.printf("%c",'#');
        }
}
//类声明,声明一个Exception的子类NoDigit
class NoDigit extends Exception   {
    public void print() {
        System.out.printf("%c", '*');
        }
}

class People {
    void printLetter(char c) throws NoLowerLetter {
        if (c < 'a' || c > 'z') {
            NoLowerLetter noLowerLetter = new NoLowerLetter();//创建NoLowerLetter类型对象
            throw noLowerLetter;
        } else {
            System.out.print(c);
        }
    }

    void printDigit(char c) throws NoDigit{
        if (c < '1' || c > '9') {
            NoDigit noDigit = new NoDigit();
            throw noDigit;
        } else {
            System.out.print(c);
        }
    }
}

public class Main{
    public static void main(String[] args) {
        People people=new People();
        for(int i=0;i<128;i++) {
            try {
                people.printLetter((char)i);
            }
            catch(NoLowerLetter e) {
                e.print();
            }
        }
        for(int i=0;i<128;i++) {
            try {
                people.printDigit((char)i);
            }
            catch(NoDigit e) {
                e.print();
            }
        }

    }
}

例题1

检查物品是否为次品

工厂检查产品次品的设备,如果发现是次品就发出警告。编程模拟设备发现次品过程。
编写一个产品类Product,有成员变量name和isDefect(是否次品),有get和set方法。
编写一个Exception的子类DefectException,该子类message属性,有构造方法DefectException() 将"次品"赋值给message成员,有toShow()方法输出message的值
编写一个Machine类,该类的方法checkProduct(Product product)当发现参数product为次品时(product的 isDefect属性为true),将抛出DefectException异常对象。
程序在主类的main方法中的try…catch语句的try部分让Machine类的实例调用checkProduct方法,如果发现次品就在try…catch语句的catch部分处理次品。

输入样例
电脑 炸药 西服 硫酸 手表 硫磺

输出样例

电脑不是次品! 电脑检查通过
次品!炸药被禁止!
西服不是次品! 西服检查通过
次品!硫酸被禁止!
手表不是次品! 手表检查通过
次品!硫磺被禁止!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
         Machine machine = new Machine();
          String name[] ;
          Scanner sc=new Scanner(System.in);
          name=sc.nextLine().split(" ");
          Product [] products = new Product[name.length]; //检查6件货物  
          for(int i= 0;i<name.length;i++) {
             products[i] = new Product();
             if(i%2==0) {
                products[i].setIsDefect(false);
                products[i].setName(name[i]);
             }
             else {
               products[i].setIsDefect(true);
               products[i].setName(name[i]);
             } 
          }
          for(int i= 0;i<products.length;i++) {
            try {
                machine.checkProduct(products[i]);
                System.out.println(products[i].getName()+"检查通过");
            }
            catch(DefectException e) {
               e.toShow();//【代码2】 //e调用toShow()方法
               System.out.println(products[i].getName()+"被禁止!"); 
            }
          }     
       } 
}

/* 将完整的Product类, DefectException类 和Machine类写在下面*/


题解


class Product { //完整编写
    boolean isDefect;
    String name;
    public String getName() {
        return name;
    }

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

    public void setIsDefect(boolean b) {
        this.isDefect=b;
    }

    public boolean isDefect() {
        return isDefect;
    }
//补充代码


}

类声明,声明一个Exception的子类DefectException
class DefectException extends Exception { //完整编写
    String message;
    DefectException(){
        message="次品!";
    }

    public void toShow() {
        System.out.printf(message);
    }


}
class Machine {
    public void checkProduct(Product product) throws DefectException {
        if(product.isDefect()) {
            DefectException defect=new DefectException();
            throw defect;
        }
        else {
            System.out.print(product.getName()+"不是次品! ");
        }
    }
}

例题2

异常-多种类型异常的捕获

如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。

补全下列代码,使得程序正常运行。

裁判测试程序

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
        String choice = sc.next();
        try {
            if (choice.equals("number"))
                throw new NumberFormatException();
            else if (choice.equals("illegal")) {
                throw new IllegalArgumentException();
            } else if (choice.equals("except")) {
                throw new Exception();
            } else
            break;
        }
        /*这里放置你的答案*/
    }//end while
    sc.close();
}

输出说明

在catch块中要输出两行信息:
第1行:要输出自定义信息。如下面输出样例的number format exception
第2行:使用System.out.println(e)输出异常信息,e是所产生的异常。

输入样例
number illegal except quit

输出样例
number format exception
java.lang.NumberFormatException
illegal argument exception
java.lang.IllegalArgumentException
other exception
java.lang.Exception

题解

		catch (NumberFormatException e){
                System.out.println("number format exception");
                System.out.println(e);
            } catch (IllegalArgumentException e){
                System.out.println("number argument exception");
                System.out.println(e);
            }
            catch (Exception e) {
                System.out.println("other exception");
                System.out.println(e);
            }

总结

在这里插入图片描述
在这里插入图片描述
!!!
出现多种异常时,Exception 必须放最后
多个异常类型必须是从小到大排列,即父类异常必须放在子类异常之后,这是因为Java会优先匹配catch语句的第一个参数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值