Java中异常处理示例

avatar 2017年04月30日23:40:12 1 2106 views
博主分享免费Java教学视频,B站账号:Java刘哥

Exception 的三个方法

  1. package com.liuyanzhao;
  2. class ZeroException {
  3.     int div(int a,int b) throws Exception{
  4.         return a/b;
  5.     }
  6. }
  7. public class Demo {
  8.     public static void main(String[] args) {
  9.         ZeroException z= new ZeroException();
  10.         try {
  11.             int x = z.div(40);
  12.             System.out.println(x);
  13.         } catch( Exception e) {
  14.             System.out.println(e.getMessage());/// by zero
  15.             System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
  16.             e.printStackTrace();/*java.lang.ArithmeticException: / by zero
  17.     at com.liuyanzhao.FushException.div(ExceptionDemo.java:7)
  18.     at com.liuyanzhao.ExceptionDemo.main(ExceptionDemo.java:15)*/
  19.             System.out.println("除0啦");//除0啦
  20.         }
  21.     }
  22. }

一个简单的try-catch语句块

  1. package com.liuyanzhao;
  2. class ZeroException{
  3.     int div(int a,int b) throws Exception{
  4.         return a/b;
  5.     }
  6. }
  7. public class Demo {
  8.     public static void main(String[] args) {
  9.         ZeroExceptionz= new ZeroException();
  10.         try {
  11.             int x = z.div(40);
  12.             System.out.println(x);
  13.         } catch( Exception e) {
  14.             System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
  15.         }
  16.     }
  17. }

ducking只是在踢皮球,方法和main都抛出

  1. package com.liuyanzhao;
  2. /*
  3.  * 1、 div()已经抛掉了异常
  4.  *       连main()抛掉了异常
  5.  *       最后只剩下Java虚拟机,你知道这家伙对异常是没有什么责任感的
  6.  *       Java虚拟机只好死给你看
  7.  * 2、 两者都躲避异常,因此没有人来处理
  8.  *       但有抛掉了,所以可以通过编译
  9.  * 3、 本例的div方法的内的异常是输入RuntimeException
  10.  *       所以它后面的声明异常(throws Exception)是可有可无的,可以省略
  11.  *       但是main方法里要么try/catch处理,要么throws抛掉
  12.  */
  13. class ZeroException{
  14.     int div(int a,int b) throws Exception {
  15.         return a/b ;
  16.     }
  17. }
  18. public class Demo {
  19.     public static void main(String[] args) throws Exception {
  20.         ZeroException z= new ZeroException();
  21.         int x = z.div(4, );
  22.         System.out.println(x);
  23.         /* 正常编译,不美观的异常
  24.          * Exception in thread "main" java.lang.ArithmeticException: / by zero   
  25.            at com.liuyanzhao.FushException.div(Demo.java:4)
  26.            at com.liuyanzhao.Demo.main(Demo.java:11)
  27.          */
  28.     }
  29. }

自定义异常信息和多个catch

  1. package com.liuyanzhao;
  2. /*
  3.  * 1、throws是在方法声明后面
  4.  *    throw是在方法里面
  5.  * 2、自定义异常信息,需要继承父类Exception
  6.  * 3、有多个catch,需要从小到大
  7.  * 4、异常也是多态的
  8.  *   别忘了异常也是对象
  9.  *   除了可以被抛出外,也没什么特别的
  10.  * 5、可以用super来处理所有异常
  11.  *   并不代表就应该这样做  
  12.  */
  13. class FushException extends Exception{
  14.     private String message;
  15.     public String getMessage() {
  16.         return message;
  17.     }
  18.     public FushException(String message) {
  19.         this.message = message;
  20.         //super(message);
  21.     }
  22.       int div(int a,int b) throws FushException {
  23.         if(b<0)
  24.             throw new FushException("除数不能为负数");
  25.         return a/b ;
  26.     }
  27. }
  28. public class Demo {
  29.     public static void main(String[] args) {
  30.         try {
  31.             FushException f = new FushException("");
  32.             int x = f.div(4,-1);
  33.             System.out.println(x);
  34.         } catch (FushException e) {
  35.             System.out.println(e.getMessage());
  36.         } catch(ArithmeticException e) {
  37.             System.out.println(e.getMessage());
  38.         } catch(Exception e) {
  39.             System.out.println(e.getMessage());
  40.         }
  41.     }
  42. }

异常处理规则

①catch和finally不能没有try

  1. public static void main(String[] args) {
  2.     //try {
  3.         FushException f = new FushException();
  4.         int x = f.div(4, -1);
  5.         System.out.println(x);
  6.     //}
  7.     catch (FushException e) {
  8.         System.out.println(e.getMessage());
  9.     }
  10. }
无法通过编译

②try与catch之间不能有程序

  1. public static void main(String[] args) {
  2.         try {
  3.             FushException f = new FushException();
  4.             int x = f.div(4, -1);
  5.             System.out.println(x);
  6.         }
  7.         int a = 1;//中间不能插入代码
  8.         catch (FushException e) {
  9.             System.out.println(e.getMessage());
  10.         }
  11.     }
无法通过编译

③try一定要有catch或finally

  1. public static void main(String[] args) {
  2.         try{
  3.             FushException f = new FushException();
  4.             int x = f.div(4,-1);
  5.             System.out.println(x);
  6.         }
  7. //      catch(FushException e) {
  8. //          System.out.println(e.getMessage());
  9. //      }
  10.         finally {
  11.             //清理
  12.         }
  13. }
这个是合法的,可以通过编译。catch和finally至少需要有一个,但还是要注意 第④项

④自带有finally的try必须要声明异常

  1. public static void main(String[] args) throws FushException{
  2.         try{
  3.             FushException f = new FushException();
  4.             int x = f.div(4,-1);
  5.             System.out.println(x);
  6.         }
  7.         finally {
  8.             //清理
  9.         }
  10.     }
main必须要throws 异常,可以编译     本文地址:http://liuyanzhao.com/3919.html 转载请注明
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0