Java中的异常处理
来源:互联网 发布:淘宝卖家如何备注订单 编辑:程序博客网 时间:2024/05/24 05:36
异常
就是程序在运行时出现不正常情况。
异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述。并封装成对象。其实就是java对不正常情况进行描述后的对象体现。
对于问题的划分:两种:一种是严重的问题,一种非严重的问题。
对于严重的,java通过Error类进行描述。对于Error一般不编写针对性的代码对其进行处理。
对于非严重的,java通过Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。
无论Error或者Exception都具有一些共性内容。
比如:不正常情况的信息,引发原因等。
java中异常的体系结构如下:
Throwable
—-|–Error
—-|–Exception
异常的处理
java 提供了特有的语句进行处理。
try
{
需要被检测的代码;
}
catch(异常类 变量)
{
处理异常的代码;(处理方式)
}
finally
{
一定会执行的语句;
}
3,对捕获到的异常对象进行常见方法操作。
String getMessage():获取异常信息。
在函数上声明异常。
便于提高安全性,让调用出进行处理。不处理编译失败。
对多异常的处理。
1,声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
2,对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
建立在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句 e.printStackTrace(),也不要简单的就书写一条输出语句。
catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
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"); }}
自定义异常
因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。所以对于这些特有的问题可以按照java的对问题封装的思想。将特有的问题。进行自定义的异常封装。
自定义异常。
需求:在本程序中,对于除数是-1,也视为是错误的是无法进行运算的。
那么就需要对这个问题进行自定义的描述。
当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。要么在内部try catch处理。要么在函数上声明让调用者处理。
一般情况在,函数内出现异常,函数上需要声明。
发现打印的结果中只有异常的名称,却没有异常的信息。
因为自定义的异常并未定义信息。
如何定义异常信息呢?
因为父类中已经把异常信息的操作都完成了。
所以子类只要在构造时,将异常信息传递给父类通过super语句。
那么就可以直接通过getMessage方法获取自定义的异常信息。
自定义异常:
必须是自定义类继承Exception。
继承Exception原因:
异常体系有一个特点:因为异常类和异常对象都被抛出。
他们都具备可抛性。这个可抛性是Throwable这个体系中独有特点。
只有这个体系中的类和对象才可以被throws和throw操作。
throws和throw的区别
throws使用在函数上。
throw使用在函数内。
throws后面跟的异常类。可以跟多个。用逗号隔开。
throw后跟的是异常对象。
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
Exceptoin中有一个特殊的子类异常RuntimeException 运行时异常。
如果在函数内抛出该异常,函数上可以不用声明,编译一样通过。
如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;
之所以不用在函数声明,是因为不需要让调用者处理。当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正。
自定义异常时:如果该异常的发生,无法在继续进行运算,
就让自定义异常继承RuntimeException。
对于异常分两种:
1,编译时被检测的异常。
2,编译时不被检测的异常(运行时异常。RuntimeException以及其子类)
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"); }}
练习:老师用电脑上课。上课中出现的问题。
比如问题是电脑蓝屏、电脑冒烟。
要对问题进行描述,封装成对象。
可是当冒烟发生后,出现讲课进度无法继续。
出现了老师的问题:课时计划无法完成
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代码块
finally代码块:定义一定执行的代码。通常用于关闭资源。
注意:
1,finally中定义的通常是 关闭资源代码。因为资源必须释放。
2,finally只有一种情况不会执行。当执行到System.exit(0);fianlly不会执行。
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"); }}
异常在子父类覆盖中的体现
1,子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2,如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集(原则就是只要父类能处理就行)。
3,如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。
异常总结
异常:是什么?是对问题的描述。将问题进行对象的封装。
异常体系:
Throwable
|–Error
|–Exception
|–RuntimeException
异常体系的特点:异常体系中的所有类以及建立的对象都具备可抛性。
也就是说可以被throw和throws关键字所操作。
只有异常体系具备这个特点。
throw和throws的用法:
throw定义在函数内,用于抛出异常对象。
throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。
当函数内有throw抛出异常对象,并未进行try处理。必须要在函数上声明,否则编译失败。
注意,RuntimeException除外。也就说,函数内如果抛出的RuntimeExcpetion异常,函数上可以不用声明。
如果函数声明了异常,调用者需要进行处理。处理方法可以throws可以try。
异常有两种:
编译时被检测异常
该异常在编译时,如果没有处理(没有抛也没有try),编译失败。
该异常被标识,代表这可以被处理。
运行时异常(编译时不检测)
在编译时,不需要处理,编译器不检查。
该异常的发生,建议不处理,让程序停止。需要对代码进行修正。
异常处理语句:
try{ //需要被检测的代码;}catch (){ //处理异常的代码;}finally{ //一定会执行的代码;}
有三个结合格式:
try { } catch () { } try { } finally { } try { } catch () { } finally { }
记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
注意:
1,finally中定义的通常是 关闭资源代码。因为资源必须释放。
2,finally只有一种情况不会执行。当执行到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放到最下面。
4,catch内,需要定义针对性的处理方式。不要简单的定义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不能抛。(不能抛比父类多的异常)
练习
注:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。
1.
写出程序结果
class Demo
{
public static void func()//throws Exception
{
try
{
throw new Exception();
}
finally
{
System.out.println(“B”);
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println(“A”);
}
catch(Exception e)
{
System.out.println(“C”);
}
System.out.println(“D”);
}
}
编译失败:
如果func放上声明了该异常。结果是?B C D
2.
写出程序结果
class Test
{
Test()
{
System.out.println(“Test”);
}
}
class Demo extends Test
{
Demo()
{
//super();
System.out.println(“Demo”);
}
public static void main(String[] args)
{
new Demo();
new Test();
}
}
Test
Demo
Test
考的子类的实例化过程。
3.
写出程序结果
interface A{}
class B implements A
{
public String func()
{
return “func”;
}
}
class Demo
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.func());
}
}
编译失败:因为A接口中并未定义func方法。
4.
写出程序结果
class Fu
{
boolean show(char a)
{
System.out.println(a);
return true;
}
}
class Demo extends Fu
{
public static void main(String[] args)
{
int i=0;
Fu f=new Demo();
Demo d=new Demo();
for(f.show(‘A’); f.show(‘B’)&&(i<2);f.show(‘C’))
{
i++;
d.show(‘D’);
}
}
boolean show(char a)
{
System.out.println(a);
return false;
}
}
A B
5.
写出程序结果
interface A{}
class B implements A
{
public String test()
{
return “yes”;
}
}
class Demo
{
static A get()
{
return new B();
}
public static void main(String[] args)
{
A a=get();
System.out.println(a.test());
}
}
编译失败,因为A接口中没有定义test方法。
6.
写出程序结果:
class Super
{
int i=0;
public Super(String a)
{
System.out.println(“A”);
i=1;
}
public Super()
{
System.out.println(“B”);
i+=2;
}
}
class Demo extends Super
{
public Demo(String a)
{
//super();
System.out.println(“C”);
i=5;
}
public static void main(String[] args)
{
int i=4;
Super d=new Demo(“A”);
System.out.println(d.i);
}
}
B C 5
7.
interface Inter
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;调用两个函数,要求用匿名内部类
Inter in = new Inter()
{
public void show(int a,int b)
{
} public void func() { } }; in.show(4,5); in.func();}
}
8.
写出程序结果
class TD
{
int y=6;
class Inner
{
static int y=3;
void show()
{
System.out.println(y);
}
}
}
class TC
{
public static void main(String[] args)
{
TD.Inner ti=new TD().new Inner();
ti.show();
}
}
编译失败,非静态内部类中不可以定义静态成员。
内部类中如果定义了静态成员,该内部类必须被静态修饰。
9.
选择题,写出错误答案错误的原因,用单行注释的方式。
class Demo
{
int show(int a,int b){return 0;}
}
下面那些函数可以存在于Demo的子类中。
A.public int show(int a,int b){return 0;}//可以,覆盖。
B.private int show(int a,int b){return 0;}//不可以,权限不够。
C.private int show(int a,long b){return 0;}//可以,和父类不是一个函数。没有覆盖,相当于重载。
D.public short show(int a,int b){return 0;}//不可以,因为该函数不可以和给定函数出现在同一类中,或者子父类中。
E.static int show(int a,int b){return 0;}//不可以,静态只能覆盖静态。
10.
写出this关键字的含义,final有哪些特点?
this:代表本类对象,哪个对象调用this所在函数,this就代表哪个对象。
final:
1,修饰类,变量(成员变量,静态变量,局部变量),函数。
2,修饰的类不可以被继承。
3,修饰的函数不可以被覆盖。
4,修饰的变量是一个常量,只能赋值一次。
5,内部类只能访问局部当中的final修饰的变量
11.
写出程序结果:
class Fu
{
int num=4;
void show()
{
System.out.println(“showFu”);
}
}
class Zi extends Fu
{
int num=5;
void show()
{
System.out.println(“showZi”);
}
}
class T
{
public static void main(String[] args)
{
Fu f=new Zi();
Zi z=new Zi();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();
}
}
成员变量不能被覆盖,随着类走
4
5
showZi
showZi
12.
interface A
{
void show();
}
interface B
{
void add(int a,int b);
}
class C implements A,B
{
//程序代码
private int a,b;//private int sum;public void add(int a,int b){ this.a =a; this.b = b; //sum = a+b;}public void show(){ System.out.println(a+b); //System.out.println(sum);}
}
class D
{
public static void main(String[] args)
{
C c=new C();
c.add(4,2);
c.show();//通过该函数打印以上两个数的和。
}
}
13.
写出程序结果
class Demo
{
public static void main(String[] args)
{
try
{
showExce();
System.out.println(“A”);
}
catch(Exception e)
{
System.out.println(“B”);
}
finally
{
System.out.println(“C”);
}
System.out.println(“D”);
}
public static void showExce()throws Exception
{
throw new Exception();
}
}
// B C D
14.
写出程序结果
class Super
{
int i=0;
public Super(String s)
{
i=1;
}
}
class Demo extends Super
{
public Demo(String s)
{
i=2; }public static void main(String[] args){ Demo d=new Demo("yes"); System.out.println(d.i);}
}
//编译失败,因为父类中缺少空参数的构造函数。
//或者子类应该通过super语句指定要调用的父类中的构造函数。
15.
写出程序结果
class Super
{
public int get(){return 4;}
}
class Demo15 extends Super
{
public long get(){return 5;}
public static void main(String[] args)
{
Super s=new Demo15();
System.out.println(s.get());
}
}
编译失败,因为子类父类中的get方法没有覆盖。但是子类调用时候不能明确返回的值是什么类型。
所以这样的函数不可以存在子父类中。
16.
写出程序结果:
class Demo
{
public static void func()
{
try
{
throw new Exception();
System.out.println(“A”);
}
catch(Exception e)
{
System.out.println(“B”);
}
}
public static void main(String[] args)
{
try
{
func();
}
catch(Exception e)
{
System.out.println(“C”);
}
System.out.println(“D”);
}
}
//编译失败。 因为打印“A”的输出语句执行不到。
记住:throw单独存在,下面不要定义语句,因为执行不到。
17.
class Demo
{
public void func()
{
//位置1;
new Inner();
}class Inner{}public static void main(String[] args){ Demo d=new Demo(); // 位置2 new Inner();//不可以,因为主函数是静态的。如果访问inner需要被static修饰。}
}
A.在位置1写 new Inner();//ok
B.在位置2写 new Inner();
C.在位置2写 new d.Inner();//错误,格式错误。 new new Demo().Inner();
D.在位置2写 new Demo.Inner();//错误,因为inner不是静态的。
18.
写出程序结果
class Exc0 extends Exception{}
class Exc1 extends Exc0{}
class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch(Exception e)
{
System.out.println(“Exception”);
}
catch(Exc0 e)
{
System.out.println(“Exc0”);
}
}
}
//编译失败。
多个catch时,父类的catch要放在下面。
19.
interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;(匿名内部类)
new Demo().show(new Test() { public void func(){} });}void show(Test t){ t.func();}
}
20.
写出程序结果
class Test
{
public static String output=”“;
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+=”1”;
}
catch(Exception e)
{
output+=”2”;
return;
}
finally
{
output+=”3”;
}
output+=”4”;
}
public static void main(String args[])
{
foo(0);
System.out.println(output);//134
foo(1);
System.out.println(output); //13423
}
}
21.
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。
class NoValueException extends RuntimeException
{
NoValueException(String message)
{
super(message);
}
}
interface Shape
{
void getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len ,int wid)//throws NoValueException{ if(len<=0 || wid<=0) throw new NoValueException("出现非法值"); this.len = len; this.wid = wid;}public void getArea(){ System.out.println(len*wid);}
}
class Circle implements Shape
{
private int radius;
public static final double PI = 3.14;
Circle(int radius){ if(radius<=0) throw new NoValueException("非法"); this.radius = radius;}public void getArea(){ System.out.println(radius*radius*PI);}
}
class ExceptionTest1
{
public static void main(String[] args)
{
Rec r = new Rec(3,4); r.getArea(); Circle c = new Circle(-8); System.out.println("over");}
}
22.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private static double pi=3.14;
private double radius;
public Circle(double r)
{
radius=r;
}
public static double compare(Circle[] cir)
{
//程序代码//其实就是在求数组中的最大值。
int max = 0;//double max = cir[0].radius; for(int x=1; x<cir.length; x++) { if(cir[x].radius>cir[max].radius) max = x; } return cir[max].radius;}
}
class TC
{
public static void main(String[] args)
{
Circle cir[]=new Circle[3];//创建了一个类类型数组里里面装的是对象,默认为null。
cir[0]=new Circle(1.0);
cir[1]=new Circle(2.0);
cir[2]=new Circle(4.0);
System.out.println(“最大的半径值是:”+Circle.compare(cir));
}
}
23.
写出程序结果
public class Demo
{
private static int j = 0;
private static boolean methodB(int k)
{
j += k;
return true;
}
public static void methodA(int i)
{
boolean b;
b = i < 10 | methodB (4);
b = i < 10 || methodB (8);
}
public static void main (String args[] )
{
methodA (0);
System.out.println(j); //4
}
}
25.
补足compare函数内的代码,不许添加其他函数。
class Circle
{
private double radius;
public Circle(double r)
{
radius=r;
}
public Circle compare(Circle cir)
{
//程序代码
/*
if(this.radius>cir.radius)
return this;
return cir;
*/
return (this.radius>cir.radius)?this: cir;
}
}
class TC
{
public static void main(String[] args)
{
Circle cir1=new Circle(1.0);
Circle cir2=new Circle(2.0);
Circle cir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println(“圆1的半径比较大”);
else
System.out.println(“圆2的半径比较大”);
}
}
- Java中的异常处理
- JAVA中的异常处理
- Java中的异常处理
- JAVA中的异常处理
- java中的异常处理
- Java中的异常处理
- java中的异常处理
- Java中的异常处理
- java中的异常处理
- Java中的异常处理
- java中的异常处理
- java中的异常处理
- Java中的异常处理
- Java中的异常处理
- Java中的异常处理
- java中的异常处理
- Java中的异常处理
- JAVA中的异常处理
- 第十六周项目1-(8)验证算法基数排序
- html中加载js文件报错:ReferenceError: $ is not defined
- 基础篇:8)产品结构设计理念(启下之章);
- AsyncTask和Handler的优缺点比较
- 使用位运算对寄存器操作的通用方法
- Java中的异常处理
- IntelliJ IDEA 里面 安装 SVN
- 软件测试资料分享,经过认真整理的一套完整教学体系
- 2 tomcat服务器学习与使用(一)
- 常见的几种矩阵分解方式
- 给UITabbarcontroller直接add子viewcontroller
- linux中的ifconfig
- 关于Integer的一个大坑
- 我的常用代码段