首页 > 其他 > 详细

JAVA学习:异常

时间:2014-02-09 15:54:50      阅读:302      评论:0      收藏:0      [点我收藏+]

一、异常官方定义:

1、就是不正常。程序在运行时出现的不正常情况。其实就是程序中出现的问题。这个问题按照面向对象思想进行描述,并封装成了对象。因为问题的产生有产生的原因、有问题的名称、有问题的描述等多个属性信息存在。当出现多属性信息最方便的方式就是将这些信息进行封装。异常就是java按照面向对象的思想将问题进行对象封装。这样就方便于操作问题以及处理问题。

2、出现的问题有很多种,比如角标越界,空指针等都是。就对这些问题进行分类。而且这些问题都有共性内容比如:每一个问题都有名称,同时还有问题描述的信息,问题出现的位置,所以可以不断的向上抽取。形成了异常体系。

 

二、异常:

1、异常:就是程序在运行时出现不正常情况。

2、异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类形式进行描述。并封装成对象。

3、其实就是java对不正常情况进行描述后的对象体现。

4、异常体系的特点:异常体系中的所有类以及建立的对象都具备可抛性。

5、也就是说可以被throw和throws关键字所操作。只有异常体系具备这个特点。

6、如果函数声明了异常,调用者需要进行处理。处理方法可以throws可以try。

7、异常有两种:

(1)编译时被检测异常

该异常在编译时,如果没有处理(没有抛也没有try),编译失败。该异常被标识,代表这可以被处理。

(2)运行时异常(编译时不检测)

在编译时,不需要处理,编译器不检查。该异常的发生,建议不处理,让程序停止。需要对代码进行修正。

 

 

对于问题的划分:两种:一种是严重的问题,一种非严重的问题。

1、对于严重的,java通过Error类进行描述。

2、对于Error一般不编写针对性的代码对其进行处理。

3、对与非严重的,java通过Exception类进行描述。

4、对于Exception可以使用针对性的处理方式进行处理。

5、无论Error或者Exception都具有一些共性内容。

比如:不正常情况的信息,引发原因等。

Throwable
|--Error
|--Exception

 


三、异常的处理

java 提供了特有的语句进行处理。

try
{
需要被检测的代码;
}
catch(异常类 变量)
{
处理异常的代码;(处理方式)
}
finally
{
一定会执行的语句;
}

 

ps:对捕获到的异常对象进行常见方法操作。

String getMessage():获取异常信息。

 

实例(一)、

 

1、

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Demo
{
    int div(int a,int b)throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题。
    {
        return a/b;
    }
}
 
 
class  ExceptionDemo
{
    public static void main(String[] args)
    {
        Demo d = new Demo();
        try
        {
            int x = d.div(4,1);
            System.out.println("x="+x);
        }
        catch (Exception e)//Exception e = new ArithmeticException();
        {
            System.out.println("除零啦");
            System.out.println(e.getMessage());//  / by zero;
            System.out.println(e.toString());// 异常名称 : 异常信息。
 
            e.printStackTrace();//异常名称,异常信息,异常出现的位置。
                            //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。
                            //打印异常的堆栈的跟踪信息。
 
 
        }      
         
 
        System.out.println("over");
 
    }
}

 

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Demo
{
    int div(int a,int b)throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题。
    {
        return a/b;
    }
}
 
 
class  ExceptionDemo1
{
    public static void main(String[] args) //throws Exception
    {
        Demo d = new Demo();
        try
        {
            int x = d.div(4,0);
            System.out.println("x="+x);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
         
         
 
        System.out.println("over");
 
    }
}

  

 

 

 

 

四、对多异常的处理:

1、声明异常时,建议声明更为具体的异常。这样处理的可以更具体。

2、对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。

3、如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。


实例(二)、

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Demo
{
    int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws的关键字声明了该功能有可能会出现问题。
    {
 
        int[] arr = new int[a];
 
        System.out.println(arr[4]);
 
        return a/b;
    }
}
 
 
class  ExceptionDemo2
{
    public static void main(String[] args) //throws Exception
    {
        Demo d = new Demo();
        try
        {
            int x = d.div(5,0);
            System.out.println("x="+x);
        }
         
        catch(Exception e)
        {
            System.out.println("hahah:"+e.toString());
        }
        catch (ArithmeticException e)
        {
            System.out.println(e.toString());
            System.out.println("被零除了!!");
 
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e.toString());
            System.out.println("角标越界啦!!");
        }
         
        /**/
         
 
        System.out.println("over");
 
    }
}

 

  

 

 

五、自定义异常:

原因:

1、因为项目中会出现特有的问题,

2、而这些问题并未被java所描述并封装对象。

3、所以对于这些特有的问题可以按照java的对问题封装的思想。

4、将特有的问题。进行自定义的异常封装。

 

需求:

1、在本程序中,对于除数是-1,也视为是错误的是无法进行运算的。

2、那么就需要对这个问题进行自定义的描述。

3、当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。

4、要么在内部try catch处理。要么在函数上声明让调用者处理。

5、一般情况在,函数内出现异常,函数上需要声明。

 

如何定义异常信息呢?

因为父类中已经把异常信息的操作都完成了。

所以子类只要在构造时,将异常信息传递给父类通过super语句。

那么就可以直接通过getMessage方法获取自定义的异常信息。

 

自定义异常:

必须是自定义类继承Exception。


继承Exception原因:
异常体系有一个特点:因为异常类和异常对象都被抛出。
他们都具备可抛性。这个可抛性是Throwable这个体系中独有特点。

只有这个体系中的类和对象才可以被throws和throw操作。

 

throw和throws的用法:

1、throw定义在函数内,用于抛出异常对象。

2、throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。

3、当函数内容有throw抛出异常对象,并未进行try处理。必须要在函数上声明,都在编译失败。

4、注意,RuntimeException除外。也就说,函数内如果抛出的RuntimeExcpetion异常,函数上可以不用声明。

 

实例(三)、

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class FuShuException extends Exception //getMessage();
{
    private int value;
 
    FuShuException()
    {
        super();
    }
    FuShuException(String msg,int value)
    {
        super(msg);
        this.value = value;
    }
 
    public int getValue()
    {
        return value;
    }
 
}
 
 
 
class Demo
{
    int div(int a,int b)throws FuShuException
    {
        if(b<0)
            throw new FuShuException("出现了除数是负数的情况------ / by fushu",b);//手动通过throw关键字抛出一个自定义异常对象。
 
        return a/b;
    }
}
 
 
class  ExceptionDemo3
{
    public static void main(String[] args)
    {
        Demo d = new Demo();
        try
        {
            int x = d.div(4,-9);
            System.out.println("x="+x);    
        }
        catch (FuShuException e)
        {
            System.out.println(e.toString());
            //System.out.println("除数出现负数了");
            System.out.println("错误的负数是:"+e.getValue());
        }
         
         
 
        System.out.println("over");
 
    }
}

 

  

 

 

 

六、RuntimeException 运行时异常:

1、如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。

2、如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;

3、之所以不用在函数声明,是因为不需要让调用者处理。

4、当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正。

 

ps:自定义异常时:如果该异常的发生,无法在继续进行运算,

就让自定义异常继承RuntimeException。


对于异常分两种:

1、编译时被检测的异常

2、编译时不被检测的异常(运行时异常。RuntimeException以及其子类)

 

实例(四)、

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class FuShuException extends RuntimeException
{
    FuShuException(String msg)
    {
        super(msg);
    }
}
class Demo
{
    int div(int a,int b)throws Exception//throws ArithmeticException
    {
         
        if(b<0)
            throw new Exception("出现了除数为负数了");
        if(b==0)
            throw new ArithmeticException("被零除啦");
        return a/b;
    }
}
 
class ExceptionDemo4
{
    public static void main(String[] args)
    {
         
        Demo d = new Demo();
         
        int x = d.div(4,-9);
        System.out.println("x="+x);    
         
        System.out.println("over");
    }
}

 

  

 

 

电脑冒烟实例(五)、

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class LanPingException extends Exception
{
    LanPingException(String message)
    {
        super(message);
    }
}
 
class MaoYanException extends Exception
{
    MaoYanException(String message)
    {
        super(message);
    }
}
 
 
class NoPlanException extends Exception
{
    NoPlanException(String msg)
    {
        super(msg);
    }
}
 
class Computer
{
    private int state = 3;
    public void run()throws LanPingException,MaoYanException
    {
        if(state==2)
            throw new LanPingException("蓝屏了");
        if(state==3)
            throw new MaoYanException("冒烟了");
 
        System.out.println("电脑运行");
    }
    public void reset()
    {
        state = 1;
        System.out.println("电脑重启");
         
    }
}
 
class Teacher
{
    private String name;
    private Computer cmpt;
 
    Teacher(String name)
    {
        this.name = name;
        cmpt = new Computer();
 
    }
 
    public void prelect()throws NoPlanException
    {
        try
        {
            cmpt.run();        
        }
        catch (LanPingException e)
        {
            cmpt.reset();
        }
        catch (MaoYanException e)
        {
             
            test();
            throw new NoPlanException("课时无法继续"+e.getMessage());
             
        }
        System.out.println("讲课");
    }
    public void test()
    {
        System.out.println("练习");
    }
 
}
 
 
 
class ExceptionTest
{
    public static void main(String[] args)
    {
        Teacher t = new Teacher("老师");
        try
        {
            t.prelect();
        }
        catch (NoPlanException e)
        {
            System.out.println(e.toString());
            System.out.println("换老师或者放假");
        }
         
    }
}

 

  

 

 

finally代码块:定义一定执行的代码、通常用于关闭资源。

注意:

1、finally中定义的通常是 关闭资源代码。因为资源必须释放。

2、finally只有一种情况不会执行。当执行到System.exit(0);fianlly不会执行。

 

实例(六)、

1、

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class FuShuException extends Exception
{
    FuShuException(String msg)
    {
        super(msg);
    }
}
 
class Demo
{
    int div(int a,int b)throws FuShuException
    {
        if(b<0)
            throw new FuShuException("除数为负数");
        return a/b;
    }
}
 
class  ExceptionDemo5
{
    public static void main(String[] args)
    {
        Demo d = new Demo();
 
        try
        {
            int x = d.div(4,-1);
            System.out.println("x="+x);
 
        }
        catch (FuShuException e)
        {
            System.out.println(e.toString());
            return;
            //System.exit(0);//系统,退出。jvm结束。
        }
        finally
        {
            System.out.println("finally");//finally中存放的是一定会被执行的代码。
        }
 
 
        System.out.println("over");
    }
}

 

  

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class NoException extends Exception
{
     
}
 
public void method()throws NoException
{
 
    连接数据库;
 
    数据操作;//throw new SQLException();
 
    关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。
 
 
    try
    {
         
        连接数据库;
 
        数据操作;//throw new SQLException();
    }
    catch (SQLException e)
    {
        会对数据库进行异常处理;
        throw new NoException();
 
    }
    finally
    {
        关闭数据库;
    }
 
}

  

 

 

 

异常格式:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
第一个格式:
try
{
     
}
catch ()
{
}
 
第二个格式:
try
{
     
}
catch ()
{
}
finally
{
 
}
 
第三个格式:
try
{
     
}
finally
{
}
//记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
 
class Demo
{
    public void method()
    {
        try
        {
            throw new Exception();
        }
        finally
        {
            //关资源。
        }
    }
}
 
 
class 
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}  

 

  

 

 

 

七、异常覆盖:

异常在子父类覆盖中的体现;

1、子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。

2、如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。

3、如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。

4、如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。

 

毕老师总结:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
异常:
是什么?是对问题的描述。将问题进行对象的封装。
------------
异常体系:
    Throwable
        |--Error
        |--Exception
            |--RuntimeException
 
异常体系的特点:异常体系中的所有类以及建立的对象都具备可抛性。
                也就是说可以被throwthrows关键字所操作。
                只有异常体系具备这个特点。
 
 
--------------
throwthrows的用法:
 
throw定义在函数内,用于抛出异常对象。
throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。
 
 
当函数内容有throw抛出异常对象,并未进行try处理。必须要在函数上声明,都在编译失败。
注意,RuntimeException除外。也就说,函数内如果抛出的RuntimeExcpetion异常,函数上可以不用声明。
--------------
 
 
如果函数声明了异常,调用者需要进行处理。处理方法可以throws可以try
 
异常有两种:
    编译时被检测异常
        该异常在编译时,如果没有处理(没有抛也没有try),编译失败。
        该异常被标识,代表这可以被处理。
    运行时异常(编译时不检测)
        在编译时,不需要处理,编译器不检查。
        该异常的发生,建议不处理,让程序停止。需要对代码进行修正。
 
 
 
--------------
异常处理语句:
try
{
    需要被检测的代码;
}
catch ()
{
    处理异常的代码;
}
finally
{
    一定会执行的代码;
}
 
有三个结合格式:
1try
    {
         
    }
    catch ()
    {
    }
 
2try
    {
         
    }
    finally
    {
     
    }
 
 
3try
    {
         
    }
    catch ()
    {
    }
    finally
    {
     
    }
 
 
 
注意:
1finally中定义的通常是 关闭资源代码。因为资源必须释放。
2finally只有一种情况不会执行。当执行到System.exit(0);fianlly不会执行。
 
--------------
 
自定义异常:
    定义类继承Exception或者RuntimeException
    1,为了让该自定义类具备可抛性。
    2,让该类具备操作异常的共性方法。
 
    当要定义自定义异常的信息时,可以使用父类已经定义好的功能。
    异常异常信息传递给父类的构造函数。
    class MyException extends Exception
    {
        MyException(String message)
        {
            super(message);
        }
    }
 
自定义异常:按照java的面向对象思想,将程序中出现的特有问题进行封装。
--------------
 
 
异常的好处:
    1,将问题进行封装。
    2,将正常流程代码和问题处理代码相分离,方便于阅读。
 
 
异常的处理原则:
    1,处理方式有两种:try 或者 throws
    2,调用到抛出异常的功能时,抛出几个,就处理几个。
        一个try对应多个catch
    3,多个catch,父类的catch放到最下面。
    4catch内,需要定义针对性的处理方式。不要简单的定义printStackTrace,输出语句。
        也不要不写。
        当捕获到的异常,本功能处理不了时,可以继续在catch中抛出。
        try
        {
            throw new AException();
        }
        catch (AException e)
        {
            throw e;
        }
 
        如果该异常处理不了,但并不属于该功能出现的异常。
        可以将异常转换后,在抛出和该功能相关的异常。
 
        或者异常可以处理,当需要将异常产生的和本功能相关的问题提供出去,
        当调用者知道。并处理。也可以将捕获异常处理后,转换新的异常。
        try
        {
            throw new AException();
        }
        catch (AException e)
        {
            // 对AException处理。
            throw new BException();
        }
 
        比如,汇款的例子。
 
     
异常的注意事项:
    在子父类覆盖时:
    1,子类抛出的异常必须是父类的异常的子类或者子集。
    2,如果父类或者接口没有异常抛出时,子类覆盖出现异常,只能try不能抛。
 
 
 
参阅
ExceptionTest.java 老师用电脑上课
ExceptionTest1.java 图形面积。
 
 
 
 
 
 
         
 
 
 
 
 
 
 
 
 
class 
{
    public static void main(String[] args)
    {
        int x = 0;
        try
        {
            x = 4;
        }
        catch ()
        {
        }
        finally
        {
            System.out.println("x="+x);
        }
         
    }
}

 

  

 

 

 

 

 

 

 

 

 

JAVA学习:异常

原文:http://www.cnblogs.com/firstrate/p/3540891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!