A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qihuan 中级黑马   /  2015-7-5 21:54  /  576 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package practice;

  2. /**
  3. * 异常:程序在运行时出现的不正常情况。
  4. * -----------------------------------------------------------------------
  5. *         1.异常由来:Java对不正常情况进行描述后的对象体现。
  6. *                         问题划分:        一种是严重的问题,一种是非严重的问题。
  7. *                         严重的通过Error类描述,非严重的Java通过Exception类进行描述。
  8. *                         对于Error一般不编写针对性的代码进行处理。对于非严重的,Java通过Exception类进行描述。
  9. *                 无论Error或Exception都具有一些共性的内容。
  10. *                 Throwable
  11. *                         |--Error
  12. *                         |--Exception
  13. *                                 |--编译时可检测的异常,只要是Exception及其子类都是编译时被检测的异常
  14. *                                 |--RuntimeException,这个异常是编译时不被检查的异常。
  15. * ------------------------------------------------------------------------
  16. *         2.异常处理:Java提供了特有的语句进行处理。
  17. *                 try {需要被检测的代码}
  18. *                 catch(异常类 变量) {处理异常的代码}
  19. *                 finally {一定会执行的语句}
  20. *                 -->        finally:通常用于关闭资源。
  21. *                 --> finally只有一种情况不会执行,就是当执行到:System.exit(0);
  22. * ------------------------------------------------------------------------
  23. *         3.对捕获到的异常对象进行常见方法操作:
  24. *                 (1)String getMessage()
  25. *                 (2)toString()
  26. *                 (3)printStackTrace()
  27. * ------------------------------------------------------------------------
  28. *         4.对于多异常的处理:
  29. *                 (1)声明异常的时候,建议声明更具体的异常,这样可以处理的更具体。
  30. *                 (2)对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。
  31. *                         如果有多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
  32. *         Tips:建议在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句e.printStackTrace(),
  33. *                     也不要简单的书写一条输出语句。
  34. * ------------------------------------------------------------------------
  35. *         5.自定义异常:* 因项目中会出现特有的问题,而这些问题并没有被Java所描述并封装对象,
  36. *                                 所以对于这些特有的问题可以按照Java的对问题封装的思想,将特有的问题,
  37. *                                 进行自定义的异常封装。
  38. *                           *        自定义异常定义异常信息:
  39. *                                         因为父类已经把异常信息的操作都已经完成了,所以子类只要在构造函数时,
  40. *                                         将异常信息通过super语句传给父类,那么就可以直接通过getMessage方法,
  41. *                                         获取自定义的异常信息了。
  42. *                           *        Tips:自定义类必须继承Exception。(因为异常类和异常对象都需要被抛出,它们都具备可抛性,
  43. *                                         这是Throwable独有的特点,只有这个体系中的类和对象才可以被throws和throw操作。)
  44. *                           *        throw和throws的区别:
  45. *                                 (1)throws使用在函数上,throw使用在函数内。
  46. *                                 (2)throws后面跟的是异常类,可以跟多个,之间用“,”隔开。throw跟的是异常对象。
  47. *                           *        实例:需求是在本程序中,对于除数是负数,也视为错误。那么就要对这个问题进行自定义的描述。
  48. * ------------------------------------------------------------------------
  49. *         6.RuntimeException:        (1)如果在函数抛出该异常,函数上可以不用声明,编译一样通过。
  50. *                                                 (2)如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。
  51. *                                                 这时因为不需要让调用者处理,当该异常发生,希望程序停止,由程序员对代码进行修正。
  52. *                                                 引出-->自定义异常时,如果该异常的发生,无法再继续运算,就让自定义异常继承RuntimeException。
  53. * ------------------------------------------------------------------------
  54. *         7.异常在子父类覆盖中的体现:
  55. *                 (1)子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法只能抛出父类的异常或者该异常的子类。
  56. *                 (2)如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
  57. *                 (3)如果父类或接口的方法中没有异常抛出,那么子类再覆盖方法是,也不可以抛出异常处理。
  58. *                         如果子类方法发生了异常,就必须进行try处理,绝对不能抛出。
  59. *
  60. * @author Qihuan
  61. *
  62. */

  63. class NegativeNumException extends Exception {
  64.         private int value;
  65.        
  66.         public NegativeNumException(String msg,int value) {
  67.                 // TODO Auto-generated constructor stub
  68.                 super(msg);
  69.                 this.value = value;
  70.         }
  71.        
  72.         public int getValue() {
  73.                 return value;
  74.         }
  75. }

  76. public class ExceptionPractice {
  77.        
  78.         public int div(int a, int b)throws NegativeNumException,ArithmeticException,ArrayIndexOutOfBoundsException{
  79.                 int[] arr = new int[a];
  80.                 System.out.println(arr[4]);
  81.                 if (b <0) {
  82.                         throw new NegativeNumException("除数负数异常",b);//手动通过throw关键字抛出一个自定义异常对象。
  83.                 }
  84.                 return a/b;
  85.         }
  86.        
  87.        
  88.         public static void main(String[] args) {
  89.                 ExceptionPractice ep = new ExceptionPractice();
  90.                
  91.                         int res = 0;
  92.                         try {
  93.                                         res = ep.div(5,-1);
  94.                         } catch (ArithmeticException e) {
  95.                                 System.out.println("0不能做除数");
  96.                         } catch (ArrayIndexOutOfBoundsException e){
  97.                                 System.out.println("脚标越界异常");
  98.                         } catch (NegativeNumException e) {
  99.                                 System.out.println("除数出现负数!"+e.getValue());
  100.                                 System.out.println(e.toString());
  101.                         } finally {//finally中存放的是一定会执行的代码。
  102.                                 System.out.println("FINALLY");
  103.                         }
  104.                         System.out.println(res);
  105.                        
  106.                         System.out.println("Over");               
  107.         }
  108. }
复制代码
  1. package practice;

  2. /**
  3. * 异常练习:
  4. *         需求:求圆和长方形的面积,针对非法数据,用异常描述。
  5. * @author Qihuan
  6. *
  7. */

  8. class NoValueException extends RuntimeException {
  9.         public NoValueException(String msg) {
  10.                 // TODO Auto-generated constructor stub
  11.                 super(msg);
  12.         }
  13. }

  14. interface sharp {
  15.         double getArea();
  16. }

  17. class Rectangle implements sharp {
  18.         private double length, width;
  19.        
  20.         public Rectangle(double length, double width) {
  21.                 // TODO Auto-generated constructor stub
  22.                 if (length <= 0 || width <= 0)
  23.                         throw new NoValueException("长宽非法!");
  24.                        
  25.                 this.length = length;
  26.                 this.width = width;
  27.         }

  28.         @Override
  29.         public double getArea() {
  30.                 // TODO Auto-generated method stub
  31.                 return length*width;
  32.         }
  33.        
  34. }

  35. class Circle implements sharp {
  36.         public static final double PI = 3.14;
  37.         private double radius;
  38.        
  39.         public Circle(double radius) {
  40.                 // TODO Auto-generated constructor stub
  41.                 if (radius <= 0)
  42.                         throw new NoValueException("半径非法!");
  43.                
  44.                 this.radius = radius;
  45.         }
  46.        
  47.         @Override
  48.         public double getArea() {
  49.                 // TODO Auto-generated method stub
  50.                 return radius*radius*PI;
  51.         }
  52. }

  53. public class ExceptionTest {
  54.         public static void main(String[] args) {
  55.                 System.out.println("长方形的面积:");
  56.                 Rectangle r = new Rectangle(-2, 5);
  57.                 System.out.println(r.getArea());
  58.                
  59.                 System.out.println("圆的面积:");
  60.                 Circle c = new Circle(0);
  61.                 System.out.println(c.getArea());
  62.         }
  63. }
复制代码



3 个回复

倒序浏览
赞一个喽!!!!!!!!
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
第二部分的代码写得很精彩。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马