请高手帮忙看看我这段JAVA异常处理小程序的错误

来源:百度知道 编辑:UC知道 时间:2024/05/22 12:35:35
public class yccl extends Exception{
yccl(){}
}
class Condition{
int abs;
void add()throws yccl{
if(abs<0){
throw new yccl();
}
}
void bdd(){
try{
for(abs=5;abs<-5;abs--){
System.out.println("abs="+abs);
}
}catch(yccl e){
System.out.println("运行至此结束,继续运行将伪利!");
}
}
public static void main(String[] args){
Condition t=new Condition();
t.bdd();
}
}

编译时错误为:
Unreachable catch block for yccl.This exception is never thrown the try statement body
本人初学JAVA,谢谢帮助!

// 在bdd()并没有使用add()异常验证,所以不会捉住异常
public class yccl extends Exception {
yccl() {
}
}

class Condition {
int abs;

void add() throws yccl {
if (abs < 0) {
throw new yccl();
}
}

void bdd() {
try {
add();
for (abs = 5; abs < -5; abs--) {
System.out.println("abs=" + abs);
add();//异常验证
}
} catch (yccl e) {
System.out.println("运行至此结束,继续运行将伪利!");
}
}

public static void main(String[] args) {
Condition t = new Condition();
t.bdd();
}
}