黑马程序员 Java 异常处理机制

---------------------- android培训、java培训、期待与您交流! ----------------------

  • Java继承体系

Java提供了丰富的异常类,这些异常类之间有严格的继承关系

Java把非正常的情况分为两种:异常(Exception)和错误(Error).

Error:一般指虚拟机相关问题,如虚拟机崩溃,虚拟机出错等这种错误无法恢复或不可捕获,将导致应用程序中断。对于Error一般不编写针对性代码对齐进行处理。

 1 /*
 2 java 提供了特有的语句进行处理
 3 try
 4 {
 5     需要被检测的代码
 6 }
 7 catch(异常类 变量)
 8 {
 9     处理异常的代码;
10 }
11 finally
12 {
13     定义一定执行的代码,通常用于关闭资源。
14 }
15 */
16 class ExceptionDemo
17 {
18     public static void main(String[] args) 
19     {
20         try
21         {
22             int i=div(4,0);//new AritchmeticException()
23             System.out.println(i);
24         }
25         catch (Exception e)//Exception e=new AritchmeticException()
26         {
27             //System.out.println("除数不能为0");
28             System.out.println(e.getMessage());// / by zero
29             System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
30             e.printStackTrace();//jvm默认的异常处理机制,就是在调用printStackTrace方法
31             /*
32             java.lang.ArithmeticException: / by zero
33            at ExceptionDemo.div(ExceptionDemo.java:36)
34            at ExceptionDemo.main(ExceptionDemo.java:22)
35             */
36         }
37         finally//finally内存放一定会执行的代码
38         {
39 
40         }
41         System.out.println("over");//over执行
42     }
43     public static int div(int x,int y)
44     {
45         return x/y;
46     }
47 }

多异常的处理

 1 /*
 2 多异常处理
 3 1.声明异常时,建议声明更具体的异常。这样处理的可以更具体
 4 2.对方声明几个异常,就对应有几个catch块。如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面
 5 
 6 */
 7 class  ExceptionDemo3     
 8 {
 9     public static void main(String[] args) 
10     {
11         try
12         {
13             int i=div(4,0);
14             System.out.println(i);
15         }
16         catch (ArithmeticException e)
17         {
18             System.out.println("除数不能为零");
19         }
20         catch(ArrayIndexOutOfBoundsException e)
21         {
22             System.out.println("数组脚标越界");
23         }
24 
25     }
26     public static int div(int x,int y) throws ArithmeticException,ArrayIndexOutOfBoundsException
27     {    
28         int[] arr =new int[x];
29         System.out.println(arr[4]);
30         return x/y;    
31     }
32 }
  • Checked异常

Check异常的两种处理方式:1.使用try{}catch(){}处理2.当不知道当前异常该如何处理,应在定义方法时,抛出异常。

使用throws抛出异常

 1 /*
 2 异常声明(throws)
 3 */
 4 class ExceptionDemo2 
 5 {
 6     public static void main(String[] args) 
 7     {
 8         int i =div(3,2);
 9     }
10     public static int div(int x,int y) throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题
11     {
12         return x/y;
13     }
14 }
15 /*
16 ExceptionDemo2.java:8: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以
17 便抛出
18                 int i =div(3,2);
19                           ^
20 1 个错误
21 */

使用throw抛出异常信息。

 1 /*
 2 throw与throws区别
 3 throws使用在函数上,throw使用在函数内
 4 throws后面跟的异常类,可以有多个,用都好隔开。throw后跟的是异常对象。
 5 因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。所以对于这些特有的问题可以按照java的问题封装的思想,
 6 将特有的问题进行自定义的异常封装。
 7 自定义异常示例
 8 需求:在本程序中,对于除数是负数,也是为是错误的是无法进行运算的。那么就需要对这个问题进行自定义描述。
 9 */
10 class FuShuException extends Exception
11 {
12     FuShuException(String msg)
13     {
14         super(msg);
15     }
16 }
17 class ExceptionDemo4 
18 {
19     public static void main(String[] args) 
20     {
21         try
22         {
23             int x=div(4,-1);
24             System.out.println(x);
25         }
26         catch (FuShuException e)
27         {
28             System.out.println(e.toString());
29         }
30     }
31     public static int div(int x,int y)throws FuShuException
32     {
33         if(y<0)
34             throw new FuShuException("除数不能为负数");
35         return x/y;
36     }
37 }

 

 自定义异常练习

 1 package Exception;
 2 
 3 public class ExceptionTest {
 4     public static void main(String[] args){
 5         Rectangle r =new Rectangle(2,5);
 6         r.getArea();
 7         Circle c =new Circle(1);
 8         c.getArea();
 9     }
10 
11 }
12 class NoValueException extends RuntimeException{
13     NoValueException(String msg){
14         super(msg);
15     }
16 }
17 interface Shape{
18     void getArea();
19 }
20 class Rectangle implements Shape{
21     private int length;
22     private int width;
23     Rectangle(int length,int width){
24         if(length<=0||width<=0)
25             throw new NoValueException("输入的值不合法");
26         this.length=length;
27         this.width=width;
28     }
29     public void getArea(){
30         System.out.println(length*width);
31     }
32 }
33 class Circle implements Shape{
34     private double radius;
35     public static final double PI=3.14;
36     Circle(double radius){
37         if(radius<=0)
38             throw new NoValueException("输入的值不合法!");
39         this.radius=radius;
40         }
41     public void getArea(){
42         System.out.println(radius*radius*PI);
43     }
44 }

 

---------------------- android培训、java培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima

转载于:https://www.cnblogs.com/malinkang1989/archive/2012/06/06/2537973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值