当前位置:首页>开发>正文

java异常的捕获

2023-07-02 19:00:10 互联网 未知 开发

 java异常的捕获

java异常的捕获

首先自定义一个异常类
public class ActionException extends Exception{
public String returnMessage
public ActionException(String returnMessage){
this.returnMessage = returnMessage
}
public String getReturnMessage(){
return this.returnMessage
}

代码中如果用到这个自定义的异常类,这里的代码只是做了个演示
private void validate(int a,int b)throws ActionException{
if(a>b){
throw new ActionException("a > b")
}
if(athrow new ActionException("a < b") } } 业务逻辑代码中 public String process(){ try{ validate(a,b) }catch(ActionException ae){ System.out.println(ae.getReturnMessage()) } }

最新文章