比如这是一个递归:
public int a(int p){
...
p--;
a(p);
...
}
那么我想要退出递归时:
public int a(int p){
...
if(p == 0){
throw new Exception("blahblah"); //抛出异常
}
p--;
a(p);
...
}
调用这个递归时:
public static void main(String[] args){
try{
a(2);
}catch(Exception e){
//抛出异常后执行
}
}
简单来说就是抛出异常后会执行catch内的语句,与递归本身的
返回值无关。
本回答被网友采纳