电脑java异常_Java:异常的处理

异常分两种大的异常类型,运行时异常和受检查异常。

用户既可以使用系统的异常类来处理异常信息,也可以创建系统的异常类的子类来自定义异常,这种方式比较灵活,虚拟机可以报出自己设置的异常信息,清楚明白。

1、运行时异常

运行时异常的特点时java编译器不去检查它,也就是说,当程序中可能出现这类异常时,即时没有有try---catch语句捕获它,也没有使用throws语句抛出它,还是会编译通过。

2、受检查异常

除了运行异常外,其他异常都属于受检查异常,这种异常的特点是要么用try...catch捕获处理,要么用throws语句声明抛出,否则编译不会通过。

3、两者的区别

运行时异常表示无法让程序恢复运行的异常,导致这种异常的原因通常是由于执行了错误的操作。一旦出现错误,建议让程序终止。

受检查异常表示程序可以处理的异常。如果抛出异常的方法本身不处理或者不能处理它,那么方法的调用者就必须去处理该异常,否则调用会出错,连编译也无法通过。当然,这两种异常都是可以通过程序来捕获并处理的。

对于运行异常,建议不要用try...catch...捕获处理,应该在程序开发调试的过程中尽量的避免,当然有一些必须要处理的,自己知道了那个部分会出现异常,而这种异常你要把它处理的你想要的结果,例如:空值处理。

异常的处理方式有三种:(1)用户完全自己处理、(2)抛给主方法,主方法再抛给虚拟机处理、(3)先抛出来,用户再自己处理

使用系统异常举例如下:

第一种处理方式(用户自己检测、捕获、处理):

classExceptionDemo

{public int method(int a,intb)

{return a/b;

}

}classExceptionDemoTest

{public static voidmain(String[] args)

{

ExceptionDemo ed= newExceptionDemo();try{int x = ed.method(4,0);

System.out.println("x ="+x);

}catch(Exception e)

{

System.out.println(e.toString()); //输出异常的类的对象

System.out.println("除零了");

}finally{

System.out.println("over");

}

}

}

第二种处理方式(用户的功能方法抛出异常,交给主方法抛出异常,最后由虚拟机JVM进行处理):

classExceptionDemo

{public int method(int a,intb) throws Exception

{return a/b;

}

}classExceptionDemoTest

{public static voidmain(String[] args) throws Exception

{

ExceptionDemo ed= newExceptionDemo();

ed.method(4,0);

System.out.println("over");

}

}

第三种处理方式(用户的功能方法抛出异常,接着由用户自己检测、捕获、处理):

classExceptionDemo

{public int method(int a,intb) throws ArithmeticException,ArrayIndexOutOfBoundsException

{int[] array = new int[a];

System.out.println(array[4]);return a/b;

}

}classExceptionDemoTest

{public static voidmain(String[] args)

{

ExceptionDemo ed= newExceptionDemo();try{//int x = ed.method(4,0);//所传两个数据均发生了异常,即数组下标越界异常和除数为0异常,但这两个//异常不能同时检测到,因为检测时有循序,但第一个检测到处理完时即停止//int x = ed.method(4,1);//检测到数组越界异常

int x = ed.method(5,0); //检测到除0异常

System.out.println("x ="+x);

}catch(ArithmeticException e)

{

System.out.println("异常的数据信息为"+e.getMessage());

System.out.println(e.toString()); //输出异常的类的对象

System.out.println("除零了");//e.printStackTrace();//其实它是虚拟机的处理机制

}catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("异常的数据信息为"+e.getMessage());

System.out.println(e.toString()); //输出异常的类的对象

System.out.println("数组角标越界异常");//e.printStackTrace();//其实它是虚拟机的处理机制

}catch(Exception e) //这个是上面的子类异常的父类异常,多用于处理不在具体的异常之外的异常

{

System.out.println(e.toString()); //输出异常的类的对象

}finally{

System.out.println("over");

}

}

}

使用系统异常类Exception的子类来自定义异常

具体举例如下:

例子1:

需求:人为自定义异常(除数不能为负数)

//继承Exception的异常类

classFuShuException extends Exception

{/*private String msg;

FuShuException(String msg)

{

this.msg = msg;

}

public String getMessage() //输出异常信息

{

return msg;

}*/ private intvaule;

FuShuException(String msg,intvaule)

{

super(msg);//父类throwable已经完成了异常的处理,子类直接可以调用,所以上面的异常初始化方法不用再写

this.vaule =vaule;

}public intgetVaule()

{returnvaule;

}

}

//异常类classExceptionDemo

{public int method(int a,intb) throws FuShuException

{if(b<0)throw new FuShuException("出现了除数是负数的情况:/by fushu",b); //通过手动throw抛出自定义异常对象

return a/b;

}

}

//主方法测试classExceptionDemoTest

{public static voidmain(String[] args)

{

ExceptionDemo ed= newExceptionDemo();try{int x = ed.method(4,-1);

System.out.println("x ="+x);

}catch(FuShuException e)

{

System.out.println(e.toString()); //输出自定义异常类,自动去调用前面的get Message();

System.out.println("错误的负数是:"+e.getVaule());//System.out.println("除数为负数了");

}finally{

System.out.println("over");

}

}

}

例子2:

异常的举例描述:老师用电脑讲课

(封装成对象)异常类:

1、电脑蓝屏(BuleScreenException),抛出后老师可以捕获解决。

2、电脑冒烟(SystemSmokingException),抛出后老师解决不了,可以转换为课程进度异常抛出,老师可以捕获解决  即产生第三个异常类NoPlanExecption

提炼需求类:

1、老师:成员变量(name)、成员方法(lecture)

2、电脑:成员方法(run、reset)

//继承Exception的电脑蓝屏异常类

classBlueScreenException extends Exception

{privateString message;publicBlueScreenException(String message)

{

super(message);

}

}

//继承Exeption的系统冒烟异常类classSystemSmokingException extends Exception

{privateString message;publicSystemSmokingException(String message)

{

super(message);

}

}

//继承Exception的转换异常类classNoPlanExecption extends Exception

{privateString message;publicNoPlanExecption(String message)

{

super(message);

}

}

//定义一个老师类classTeacher

{privateString name;privateComputer c;publicTeacher(String name)

{this.name =name;

c= new Computer(3); //设置电脑目前的状态(1或2或3)

}voidlecture() throws NoPlanExecption

{try{

c.run();

}catch(BlueScreenException e)

{

System.out.println(e.toString());

c.reset();

}catch(SystemSmokingException e)

{

test();throw new NoPlanExecption("讲课不能继续,"+e.getMessage()); //冒烟异常老师解决不了,转换为课程进度异常抛出

}

System.out.println(name+"讲课");

}public voidtest()

{

System.out.println("学生自己做练习");

}

}

//定义一个电脑类classComputer

{private int state; //电脑状态:数字1代表正常运行、数字2代表蓝屏、数字3代表冒烟

public Computer(intstate)

{this.state =state;

}voidrun() throws BlueScreenException,SystemSmokingException

{if(state==2)throw new BlueScreenException("电脑蓝屏了");else if(state==3)throw new SystemSmokingException("电脑冒烟了");

System.out.println("电脑运行");

}public voidreset()

{

state= 1; //重新将电脑状态设置成数字1

System.out.println("电脑重新启动");

}

}

//测试类classCustomizeExceptionDemo

{public static voidmain(String[] args)

{

Teacher t= new Teacher("毕老师");try{

t.lecture();

}catch(NoPlanExecption e)

{

System.out.println(e.toString());

System.out.println("换老师或放假");

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值