【Java随笔】异常try/catch/finally,throws,自定义异常使用

1.try/catch/finally,throw使用

public class Test20 {
    /*
    * 1.异常:出现异常后程序中断,后续代码不执行。
    * 2.异常处理:出现异常,抛出异常,后续代码继续执行
    * try
    *   {可能出现异常的代码块}
    * catch(Exception e)//可以放多个catch,捕获异常范围从小到大,因为按顺序执行。
    *   {当try代码块中存在异常,执行这里。try中代码块后续不再执行}
    * finally
    *   {不论是否产生异常,都执行此代码块}。
    * 3.异常种类:Throwable基类,Error、Exception(编译期异常、运行时异常RuntimeException)
    * */
    public static void main(String[] args) {
        //div(1,1);
        //div(1,0);
        /*try {
            div2(1,0);
        } catch (Exception e) {
            throw new RuntimeException(e);//抛给虚拟机,后续代码不会执行
        }*/
        try {
            div2(1,0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("异常之后会执行吗?");
    }
    private static int div(int num1,int num2){
        try{
            //int[] arr = {1,2,3,4,5};
            //System.out.println(arr[5]);
            int result = num1/num2;
            System.out.println(result);
            return result;
        }catch(ArrayIndexOutOfBoundsException e){
            //e.printStackTrace();
            System.out.println("数组越界");
        }catch(ArithmeticException a){
            a.printStackTrace();
            System.out.println("除数为0");
        }catch (Exception exception){
            exception.printStackTrace();
            System.out.println("存在异常");
        }finally {
            System.out.println("finally");
        }
        return -1;
    }
    /*
    * throws在方法声明处使用,
    * throw在方法内使用(在catch中处理,若不处理,会交给虚拟机,中断程序)
    * */
    private static int div2(int a,int b) throws Exception{
        try{
            int c = a/b;
            return c;
        }catch (Exception e){
            throw new RuntimeException();
        }


    }
}

2.自定义异常使用场景

import java.util.Scanner;

public class Test21 {
    /*
    * 自定义异常类:异常类本身没有实际功能,只是提示作用。
    * 1.通常继承 Exception、RuntimeException、Throwable
    * 2.实现方法:重写父类构造方法
    * 3.使用场景:用异常来处理业务逻辑。当用户登录不成功时,使用异常来提示。
    * */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        UserService1 us = new UserService1();
        try {
            User user = us.login(name,password);
            System.out.println("登录成功");
            System.out.println(user.toString());
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
}
//自定义异常类
class MyException extends Exception{
    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }
}
//业务类:用户服务,登录成功返回用户
class UserService1{
    public User login(String username,String password) throws MyException{
        if (!"admin".equals(username)) {
            throw new MyException("用户名错误");
        }
        if (!"12345".equals(password)) {
            throw new MyException("密码错误");
        }
        User user = new User("admin","12345",18,"男");
        return user;
    }
}
class User{
  private String username;
  private String password;
  private int age;
  private String sex;

    public User() {
    }

    public User(String username, String password, int age, String sex) {
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }

    public User(String name, String password) {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}


3.assert使用

在idea中run—>edit configurations—>modify option中加入VM options,输入-ea
在这里插入图片描述

public class Test22 {
    public static void main(String[] args) {
        int add = add(1, 1);
        //如果add不等于3,会跳转到异常
        assert add==3:"结果错误";
    }
    private static int add(int a,int b){
        return a+b;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值