黑马程序员技术交流社区

标题: Java 异常/多异常的处理 [打印本页]

作者: _Water    时间: 2014-4-2 21:37
标题: Java 异常/多异常的处理
  1. /*一.异常的处理:
  2. java 提供了特有的语句进行处理
  3. try
  4. {
  5.         需要被检测的代码
  6. }
  7. catch(异常类 变量){
  8.         处理异常的语句
  9. }
  10. finally{
  11.         一定会执行的语句
  12. }

  13. 二.对捕获的异常对象进行常见方法操作
  14. String getMessage();获取异常信息
  15. 类Excetion 是继承于Throwable类的
  16. class Throwable
  17. {
  18.         private String message;
  19.         Throwable(String message)
  20.         {
  21.                 this.message=message;
  22.         }
  23.         public String getMessage()
  24.         {
  25.                 return message;
  26.         }
  27. }

  28. class Exception extends Throwable
  29. {
  30.         Exception (String message)
  31.         {
  32.                 super(message);
  33.         }
  34. }

  35. 在函数上声明异常的好处:
  36. 1.提高安全性
  37. 2.让调用者进行处理,不处理编译失败

  38. 三.对多异常的处理:(函数中只要有异常发生就不会进行了,要退出了)
  39. 1.声明异常时,建议声明更为具体的异常,这样处理的更具体ArithmeticException
  40. 2.对方声明几个异常,就对应有几个catch块(不要定义多余的catch块)
  41.         如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面

  42. 独立在进行catch处理时,catch中一定要定义具体的处理方式。
  43. 不要简单定义一句e。printStackTrace();
  44. 也不要简单的就书写一条输出语句。

  45. */
  46. class Demo
  47. {
  48.         int div(int a ,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws的关键字声明了该功能可能会出现问题
  49.         {
  50.                 int[] arr =new int[a];
  51.                 System.out.println(arr[2]);
  52.                 return a/b;
  53.         }
  54. }

  55. class ExceptionDemo
  56. {
  57.         public static void main(String[] args)
  58.         {
  59.                 Demo d =new Demo();
  60.                
  61.                 try
  62.                 {
  63.                         int x =d.div(2,2);       
  64.                         System.out.println(x);
  65.                 }
  66.                 catch (ArithmeticException e)//Exception e =new ArithmetricException();多态
  67.                 {
  68.                         System.out.println("div by zero ");
  69.                         System.out.println(e.getMessage()); // / by zero;
  70.                         System.out.println(e.toString());//异常名称:异常信息

  71.                         e.printStackTrace();//异常名称:异常信息,异常出现的位置
  72.                                 //其实JVM 默认的异常处理机制,就是调用printStackTrace方法
  73.                                 //打印异常的堆栈的跟踪信息
  74.                 }
  75.                 catch(ArrayIndexOutOfBoundsException e)
  76.                 {
  77.                         System.out.println(e.toString());
  78.                         System.out.println("jiaobiao越界");
  79.                 }
  80.                
  81.                 System.out.println("end");
  82.                 //byte[] arr =new byte[1024*1024*341];//在内存分配了341M,
  83.                 //Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  84.         }
  85. }
复制代码

作者: dark_lin    时间: 2014-4-2 22:33
学习了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2