您的位置:首页 > 编程语言 > Java开发

JAVA学习日志(9-3-异常练习)

2016-08-02 00:00 183 查看
Exception中有一个特殊的子类异常 RuntimeException 运行时异常

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

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

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

当该异常发生,希望程序停止

因为在运行时,出现了无法继续运算的情况,希望停止程序后

对代码进行修正

class rDemo{
int div(int a,int b){
if(b==0){
throw new ArithmeticException("除数为零");
}
return a/b;
}
}
class ExceptionDemo3{
public static void main(String[] args){
rDemo r=new rDemo();
int x=r.div(4,0);
System.out.println("x="+x);
System.out.println("over");
}
}
/*编译通过,输出结果为:
Exception in thread "main" java.lang.ArithmeticException: 除数为零
at rDemo.div(ExceptionDemo3.java:4)
at ExceptionDemo3.main(ExceptionDemo3.java:12)
*/

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

就让自定义异常继承RuntimeException

class FuShuException extends RuntimeException{
FuShuException(String msg){
super(msg);
}
}

class Demo{
int div(int a,int b){
if(b<0){
throw new FuShuException("除数为负数");
}
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,-1);
System.out.println("x="+x);
System.out.println("OVER");
}
}

对于异常分为两种:

1.编译时被检测的异常

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

异常练习

v1.0

/*
需求:老师用电脑上课

*/
class Computer{
public void run(){
System.out.println("Computer_Run");
}
public void reset(){
System.out.println("Computer_Reset");
}
}
class Teacher{
private String name;
private Computer cmpt;
Teacher(String name){
this.name=name;
cmpt=new Computer();
}
public void prelect(){
cmpt.run();
System.out.println("Teacher_Run");
}
}
class ExceptionTest{
public static void main(String[] args){
Teacher t=new Teacher("毕老师");
t.prelect();
}
}

v2.0 加入异常

/*
需求:老师用电脑上课
上课中的异常:电脑蓝屏 电脑冒烟
*/
class bluesrException extends Exception{	//将异常进行描述,封装成对象
bluesrException(String message){
super(message);
}
}
class smokeException extends Exception{	//冒烟发生后讲课进度无法继续
smokeException(String message){		//出现讲师异常
super(message);
}
}
class noPlanException extends Exception{
noPlanException(String message){
super(message);
}
}
class Computer{
private int state=3;
public void run()throws bluesrException,smokeException{
if(state==2){
throw new bluesrException("蓝屏");
}
if(state==3){
throw new smokeException("冒烟");
}
System.out.println("Computer_Run");
}
public void reset(){
state=1;
System.out.println("Computer_Reset");
}
}
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(bluesrException e){
cmpt.reset();
}
catch(smokeException e){			 //抛出讲师异常
test();
throw new noPlanException("课时无法继续"+e.getMessage());		//和return一样属于结束语句
}
System.out.println("Teacher_Run");
}
public void test(){
System.out.println("Test");
}
}
class ExceptionTest{
public static void main(String[] args){
Teacher t=new Teacher("毕老师");
try{
t.prelect();
}
catch(noPlanException e){
System.out.println(e.toString())
3ff0
;
System.out.println("换老师或者放假");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: