温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JAVA异常处理方式是什么

发布时间:2021-12-30 15:33:12 来源:亿速云 阅读:112 作者:iii 栏目:编程语言

这篇文章主要讲解了“JAVA异常处理方式是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JAVA异常处理方式是什么”吧!

1:try-catch

结构:

*  结构:
 * try{
 * 可能出现异常de代码段
 * }catch(可能出现的异常){
 * 解决办法
 * }

 
顺序:

*  try-catch的执行顺序:
 *   1、执行try块中的代码块  如果出现异常
 *    2、通过出现的异常去匹配 catch中声明的异常类型
 *    3、如果匹配成功 执行catch中的代码块         如果匹配失败  jvm处理当前异常信息 (终止程序 输出异常信息)
 *    4、继续执行剩下的代码

 
例子:

  1. public class Test04 {  

  2.     public static void main(String[] args) {  

  3.         try{  

  4.             int num = 1/0;//new ArithmeticException()  

  5.             System.out.println(num);  

  6.         }catch(InputMismatchException e){ //InputMismatchException e = new ArithmeticException();  

  7.             System.out.println("除数不能为0");  

  8.         }  

  9.           

  10.         System.out.println("嘿嘿");  

  11.           

  12.     }  

  13. }  

 
注意事项

PS:只能处理一种异常信息。

 

2:try-多重catch

结构:

*  结构:
 *   try{
 *   可能出现异常的代码
 *   }catch(异常类型1 e1){
 *   解决方案1
 *   }catch(异常类型2 e2){
 *   解决方案2
 *   }catch(异常类型3 e3){
 *   解决方案3
 *   }。。。。{
 *   }

 
顺序

* 执行顺序:
 * 1、执行try块 如果出现异常
 * 2、以此匹配多重catch中声明的异常
 * 3、如果匹配成功 执行当前匹配成功的catch块 try-catch块执行完毕 继续执行下面的代码
 * 4、如果匹配失败 交由jvm处理 程序终止 输出异常信息
 * 5、一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息

 
例子

  1. public class Test05 {  

  2.     public static void main(String[] args) {  

  3.           

  4.         Scanner input = new Scanner(System.in);  

  5.           

  6.         try{  

  7.             System.out.println("请输入被除数---->");  

  8.             int num1 = input.nextInt();  

  9.             System.out.println("请输入除数---->");  

  10.             int num2 = input.nextInt();  

  11.             System.out.println(num1+"/"+num2+"="+(num1/num2));  

  12.               

  13.         }catch(InputMismatchException e){//这个异常对象中没有维护异常的原因 所以通过getMessage获取不到异常信息  null值  

  14.             //e.printStackTrace();   

  15.             System.out.println(e.getMessage());  

  16.             System.out.println("用户输入有误");  

  17.         }catch(ArithmeticException e){//这个异常对象中维护异常的原因 所以通过getMessage可以获取到异常信息  

  18.             System.out.println(e.getMessage());//by zero  

  19.             System.out.println("除数不能为0");  

  20.               

  21.               

  22.         }catch(Exception e){//Exception e = new 可能出现的异常();   父类变量指向了子类对象   

  23.             //多态  

  24.             System.out.println(e.getMessage());  

  25.             System.out.println("外星人把页面叼走了 请等待。。。");  

  26.         }  

  27.           

  28.     }  

  29. }  

 
注意事项:

PS:
  1. 一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息

  2. 常见的异常的对象中的方法:

* 异常中常见的方法:
      * e.getMessage() -->获取异常的原因藐视
      * e.printStackTrace()  -->打印异常的出现行数以及异常的全限定名* e.toString  --> 异常的全限定名

 

3:try-多重catch-finally

结构:

* 结构:
 * try{
 *   可能出现异常的代码
 *   }catch(异常类型1 e1){
 *   解决方案1
 *   }catch(异常类型2 e2){
 *   解决方案2
 *   }catch(异常类型3 e3){
 *   解决方案3
 *   }。。。。{
 *   }finally{
 *   代码块
 *   }

 
顺序:

* 执行顺序:
 * 1、执行try块 如果出现异常
 * 2、以此匹配多重catch中声明的异常
 * 3、如果匹配成功 执行当前匹配成功的catch块  执行finally代码块 try-catch-finally块执行完毕 继续执行下面的代码
 * 4、如果匹配失败 交由jvm处理 程序终止 输出异常信息 也会执行finally代码块
 * 5、一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息
 * 6、一般情况下通过finally去关闭连接资源

 
例子:

  1. public class Test06 {  

  2.     public static void main(String[] args) {  

  3.         Scanner input  = null;  

  4.         try{  

  5.               

  6.             input = new Scanner(System.in);  

  7.             System.out.println("请输入被除数---->");  

  8.             int num1 = input.nextInt();  

  9.             System.out.println("请输入除数---->");  

  10.             int num2 = input.nextInt();  

  11.             System.exit(0);//关闭虚拟机 0正常退出  非0 强制退出  

  12.             System.out.println(num1+"/"+num2+"="+(num1/num2));  

  13.               

  14.         }catch(InputMismatchException e){  

  15.             System.out.println("用户输入有误");  

  16.         }catch(ArithmeticException e){  

  17.             System.out.println("除数不能为0");  

  18.         }catch(Exception e){  

  19.             System.out.println("外星人把页面叼走了 请等待。。。");  

  20.         }finally{  

  21.               

  22.             System.out.println("我被执行了");  

  23.             //在这里关闭的  

  24.             input.close();  

  25.         }  

  26.           

  27.     }  

  28. }  

 
注意事项:

PS:
  1. finally一定会被执行 return 以及异常或者是正常情况下都会执行finally代码

  2. System.exit(数字) 退出虚拟机 0 正常 非0 强制

 
 

4:throws 声明一个异常

语法格式:

* 注意格式:
 * 方法() throws 异常类型1,异常类型2。。。{}

 
注意事项:

  1. s不要忘记 一个方法可以声明多个异常信息

  2. 某个方法如果对外声明一个异常,那么调用者一定要解决当前异常。解决方案:

    A、try-catch     B、继续向外声明

 
案例:

  1. public class Test08 {  

  2.     public static void main(String[] args)throws Exception {  

  3.         /*try{

  4.              

  5.             //1、调用add方法

  6.             add(1,2);

  7.         }catch(Exception e){

  8.              

  9.         }*/  

  10.         System.out.println(add(1,43));  

  11.           

  12.     }  

  13.       

  14.     /*

  15.      * 计算两个数相加

  16.      * 调用这个方法可能出现异常

  17.      * 这个方法就必须对外声明一个异常

  18.      */  

  19.     public static int add(int num1,int num2)throws Exception{  

  20.         return num1+num2;  

  21.     }  

  22.       

  23. }  

 

5:throw抛出异常信息

语法格式:

throw new 异常类型();
PS:抛出异常是在方法内部编写的

 
注意事项:

  1. throw 抛出异常在方法体体编写

  2. 一般情况下和throws一起使用

 
案例:

  1. public class Test09 {  

  2.     public static void main(String[] args) {  

  3.         //1、创建一个user对象  

  4.         User u = new User();  

  5.         //2、解决异常  

  6.         try {  

  7.             u.setGender(12);//new Exception();  

  8.         } catch (Exception e) {  

  9.             e.printStackTrace();  

  10.         }  

  11.           

  12.           

  13.     }  

  14.       

  15.       

  16. }  

  17. class User{  

  18.       

  19.     private int gender;  

  20.       

  21.     public User() {  

  22.         // TODO Auto-generated constructor stub  

  23.     }  

  24.   

  25.     public int getGender() {  

  26.         return gender;  

  27.     }  

  28.   

  29.     public void setGender(int gender) throws Exception{  

  30.         //判定gender的值  

  31.         if(gender==0||gender==1){  

  32.             this.gender = gender;  

  33.         }else{  

  34.             //抛出一个异常  

  35.             throw new Exception();  

  36.         }  

  37.     }  

  38. }  

6:自定义异常:

自定义异常的步骤:

* 如何自定义异常:
 * 1、创建一个类 让当前类要么继承Exception 要么继承RuntimeException
 *  2、编写当前类的构造器  :
 *   a、一定要写空构造器
 *   b、一定要写一个带异常原因描述的构造器 (带一个String参数的构造器)
 *  3、在构造器内部通过super()调用父类的构造器即可

 
自定义异常如何获取异常信息:类图:
JAVA异常处理方式是什么 
实例:

  1. public class GenderException extends Exception{  

  2.       

  3.     public GenderException(){  

  4.           

  5.     }  

  6.       

  7.     public GenderException(String str){  

  8.         super(str);  

  9.     }  

  10.       

  11. }  

 
测试类:

  1. public class Test11 {  

  2.     public static void main(String[] args) {  

  3.           

  4.           

  5.         //1、创建一个Person对象  

  6.         Person p = new Person();  

  7.         try{  

  8.             p.setGender(10);  

  9.               

  10.         }catch(GenderException e){  

  11.             System.out.println(e.getMessage());  

  12.         }  

  13.           

  14.         System.out.println(p.getGender()==0?"女生":"男生");  

  15.           

  16.           

  17.     }  

  18. }  

  19. class Person{  

  20.       

  21.     private int gender;  

  22.       

  23.     public Person() {  

  24.         // TODO Auto-generated constructor stub  

  25.     }  

  26.   

  27.     public int getGender() {  

  28.         return gender;  

  29.     }  

  30.   

  31.     public void setGender(int gender) throws GenderException,NullPointerException{  

  32.         if(gender==0||gender==1){  

  33.             this.gender = gender;             

  34.         }else{  

  35.             //抛出异常  

  36.             throw new GenderException("性别赋值错误");  

  37.         }  

  38.     }  

  39.       

  40.       

  41.       

  42.       

  43. }  

 
PS:当int作为属性时它是具有默认值,默认值是0.而这个值有可能导致程序运行期间出现不稳定因素

感谢各位的阅读,以上就是“JAVA异常处理方式是什么”的内容了,经过本文的学习后,相信大家对JAVA异常处理方式是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI