ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java 异常

2022-05-22 16:32:01  阅读:125  来源: 互联网

标签:username Java String int password 异常 public


Throwable是Error和Exception的父类,在开发中只关注Exception

Exception可分为编译期异常(受检异常)和运行期异常(非受检异常)

异常会导致程序中断,无法继续执行。处理异常可以让程序保持运行状态

在开发中常把 try 将可能出现异常的代码块包裹起来。使用 catch 捕捉异常,catch可以有多个,异常子类放前面,异常父类放后面。

 

异常处理过程分析:

  (1)一旦产生异常,则系统会自动生成一个异常类的实例化对象

  (2)如果异常发生在try语句块内,系统会自动寻找匹配的catch代码块执行。若异常产生之后没有异常处理语句,则程序就会退出,并报告异常错误

 

 

代码示例:

public class TestException {
    public static void main(String[] args) {
        
        /*
         * 值得注意的是:在try代码块中,若某行代码发生异常,那么程序立即跳转至处理异常的代码块(比如catch代码块)
         * 发生异常所在行后面的代码将无法正常执行
         * finally代码块无论是否发生异常,都会执行
         */
        try {
            int a=6;
            int b=2;
            int c= a/b;
            int [] arr = {1,2,3,4};
            System.out.println(arr.length);
            System.out.println(arr[3]);
            arr = null;
            System.out.println(arr[3]);
        }
        catch(ArithmeticException e) {
            System.out.println("发生算数异常");
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("发生数组越界异常");
        }
        catch(NullPointerException e) {
            System.out.println("发生空指针异常");
        }
        catch(Exception e) {
       e.printStackTrace(); System.out.println("程序发生异常"); } finally { System.out.println("程序运行完毕"); } } }

 

throw与throws关键字(抛出异常,并不是处理异常)

  (1)throws关键字用在方法的声明表示方法中并不处理异常,而交给调用方法处进行处理。对于Java程序而言,如果没有加入任何的异常处理,发生的异常则由JVM处理(不处理,显示报错)

  (2)throw表示在方法中手动抛出一个异常。从异常处理的机制来看,所有的异常一经产生,系统实际上抛出的是一个异常类的实例化对象,那么此对象也可以由throw直接抛出

public class TestThrows {

    public static void main(String[] args) {
        try{
            divide(6,0);
        }
        catch(ArithmeticException e) {
            System.out.println("发生了算术异常");
        }
    }
    
    //throws和throw 常搭配使用
    public static int divide(int a,int b) throws ArithmeticException{
        
        try{
            int c = a/b;
            return c;
        }
        catch(ArithmeticException e) {
            throw new ArithmeticException("发生了算术异常");
        }        
    }
}

 

 自定义异常

  自定义异常通常是继承一个异常类(Throwable、Exception、RuntimeException)来实现。

  自定义异常,使用提供构造方法来实现。

  异常对象本身没有实际功能,仅是一个具有特殊意义的标识

 

public class TestMyexception2 {
    public static void main(String[] args) {
        UserService1 us1 = new UserService1();
        try {
            us1.login("", "");
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
}

//自定义异常,继承异常类
class MyException extends Exception{
    
    public MyException() {
        super();
    }
    
    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(!"123".equals(password)) {
            throw new MyException("密码错误");
        }
        return new User(username,password,18);
    }
}


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

 

受检异常:Exception

  在定义方法时,必须声明出所有可能抛出的Exception;

  调用该方法时,必须捕获它的checked exception,不然就得把exception传递下去

  exception是从 java.lang.Exception中衍生出来的。例如:IOException 、SQLException等等

 

非受检异常:RuntimeException

  在定义方法时不需要声明会抛出runtime exception;

  在调用这个方法时不需要捕获这个runtime exception;

  runtime exception是从 java.lang.RuntimeException 或 java.lang.Error中衍生出来的。例如:NullPointerException 、ArrayIndexOutOfBoundsException等等

 

标签:username,Java,String,int,password,异常,public
来源: https://www.cnblogs.com/javafufeng/p/16298167.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有