Java异常处理

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

异常处理

异常是阻止当前方法或作用域继续执行的问题在程序中导致程序中断运行的一些指令。

try——catch——finally异常处理

 

 

 

/*
    1.Throwable是异常的基类分为Error和Exception,在编程中我们关注Exception
    2.Exception分为编译期异常受检和运行期异常非受检
    3.异常会导致程序中断无法继续执行
    4.在开发中我们需要把可能出现异常的代码使用try语句块包括起来
 */
public class test {
    public static void main(String[] args) {
        //div(10,2);
        //div(10,0);
        method();
    }

    //除法运算
    private static void div(int num1,int num2){
        int[] arr={1,2,3,4,5};
        try{
            System.out.println(arr[2]);
            arr =null;
            System.out.println(arr.length);
            int result = num1 / num2;
            System.out.println(result);
        }
        //匹配异常从第一个开始匹配所以小的异常应该在前面
        catch (ArithmeticException e){
            System.out.println("除数不能为0");
        }
        catch (ArrayIndexOutOfBoundsException e){
            System.out.println("数组越界");
        }
        catch(NullPointerException e){
            System.out.println("空指针异常");
        }
        catch(Exception e){
            System.out.println("出错了");
        }
        finally {
            System.out.println("程序执行完毕 ");
        }

        System.out.println("程序结束");



    }

    private static int method(){
        int a=10;
        int b=5;
        try{
            System.out.println("a="+a);
            System.out.println("b="+b);
            int c =a/b;
            System.out.println("a/b="+c);
            //return之前会去先进行catchfinally的判断如果有finally便会执行finally
            return c;
        }
        catch(Exception e){
            //代码测试时使用
            e.printStackTrace();
        }
        finally{
            System.out.println("finally!");
        }
        return -1;
    }

  throw与throws关键字

throw在方法内使用throws在方法声明中使用。

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

public class test {
    public static void main(String[] args) {
        //div(20,0);

        input();

    }

    public static int div(int a, int b) throws ArithmeticException {
        try {
            int c = a / b;
            return c;
        } catch (ArithmeticException e) {
            throw new ArithmeticException("除数不能为0");
        } finally {
            System.out.println("运行结束");
        }
    }

    private static void input() {
        Scanner input = new Scanner(System.in);
        try {
            int number = input.nextInt();
            System.out.println(number);
        } catch (InputMismatchException e) {
            System.out.println("输入不匹配");
        }
    }
}

 自定义异常

 

 

MyException.java

package test;

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

 User.java

package test;

public class User {
    private String username;
    private String password;
    private String sex;
    private int age;

    public User(String username,String password,int age,String sex){
        this.sex = sex;
        this.age = age;
        this.password = password;
        this.username = username;
    }
    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 String getSex() {
        return sex;
    }

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

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

    public int getAge(){
        return age;
    }

    public String toString(){
        return "User"+username+password+age+sex;
    }
}

 UserService.java

package test;

public class UserService {
    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",19,"男");
        return user;
        }
}

 login.java

package test;

import java.util.Scanner;

public class login {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = input.nextLine();
        System.out.println("请输入密码");
        String pass = input.nextLine();

        UserService us = new UserService();
        try{
            User user = us.login(name, pass);
            System.out.println("登陆成功");
            System.out.println((user));
        }
        catch(MyException e){
            e.printStackTrace();
        }

    }
}

受检与非受检异常Exception与RuntimeException

Exception受检异常在编程期检查在调用抛出这个异常的方法时必须显示的使用try...catch...

RuntimeException非受检异常在运行期检查在调用抛出这个异常的方法时必须不用显示的使用try...catch...

在使用自定义异常时根据实际的业务要求来决定使用那个作为父类

测试人员使用较多 

 Debug技术

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java