【异常六】什么时候处理与抛出异常

/ Java / 没有评论 / 2321浏览

采用伪代码的形式模拟JAVA代码,不能编译通过,重在思想.

什么时候处理异常

  1. 给客户端(第三方)提供服务时,在服务器的最顶层进行异常处理
try{
   // 这里抛出异常
   return {
         status : 1 ,
         data : "其他数据"
   };
}catch(Exception ex){
    return {
         status : 0 ,
         msg : ex.getMessage(),
    };
}
  1. 在与服务器端(第三方)进行交互时,将第三方异常转换为本地异常并进行处理
var data = "";
try{
    data = Request(serverUrl,reqData);
}
catch(Exception ex){
	// 进行一定的异常处理,处理系统级别异常
    throw new RuntimeException("连接失败",ex);
}
// 检测服务器异常
if(data.status < 1){
    throw new RuntimeException(data.msg);
    // 或者:  throw new RuntimeException(
    //      String.format("%s [%d] \n请求内容 : %s", msg, data.status, reqData));
}
  1. 即使出了异常,也需要获取到默认值,通常用于数据类型转换或请求其他服务器。
String str = "a";
int ret = 0;
try{
    ret = Integer.parse(str);
}catch(Exception ex){
}
return ret;
  1. 将Exception升级或降级处理
try{
    throw new Exception("异常")
}catch(Exception ex){
    throw new RunttimeException(ex.getMessage, ex);
}
  1. 线程异常处理,并进行交互
Thread thread = new Thread(new Runable(){
    public void run(){
      try{
         // 调用其他方法
      }catch(Exception ex){
         // 这里进行异常处理 
         // 通常可以把异常进行打印或交互到其他线程进行处理
         // 如:后台开了多线程处理数据,数据线程出现异常,
         // 则把异常交给主线程处理
      }
    }
});

什么时候抛出异常

  1. 客户端处理服务器数据时,抛出异常
var data = "";
try{
    data = Request(serverUrl,reqData);
}
catch(Exception ex){
	// 进行一定的异常处理,处理系统级别异常
    throw new RuntimeException("连接失败",ex);
}
// 检测服务器异常
if(data.status < 1){
    throw new RuntimeException(data.msg);
    // 或者:  throw new RuntimeException(
    //     String.format("%s [%d] \n请求内容 : %s", msg, data.status, reqData));
}
  1. 默认值判断,抛出异常。如: 0 、null 、""
String str = RequestServer(url,data);
if(StringHelper.IsEmpty(str)){
    throw new RunttimeException("服务器返回值为空");
}
  1. 异常升级降级时抛出新异常
try{
    throw new Exception("异常")
}catch(Exception ex){
    throw new RunttimeException(ex.getMessage, ex);
}
  1. 业务异常
switch(type){
    case 0:
    case 1:
        break;
    default:
        throw new RunttimeException("你的类型"+type+"不支持");
}
  1. 将当前线程转换到另外的线程,并抛出异常
public class Temp{
    private boolean isEnd = false;
    private Exception exception;

    private void TheadHandle(){
        Thread thread = new Thread(new Runable(){
            public void run(){
                try{
                    // 调用其他方法
                }catch(Exception ex){
                    exception = ex;
                }
                isEnd = true;
            }
        });
        thread.start();
    }
    
    private void Wait(){
        while(!isEnd){
            ThreadHelper.Sleep(10);
        }
        if(exception != null){
            throw exception;
        }
    }
}

自定义数据抛出异常

有的时候我们需要自定义异常,然后将数据放到异常里面,将数据直接交给最顶层进行处理.

private void throwExceptionSub(){
     throw new ExceptionData({
          code : -1,
          msg : "执行错误" ,
          data : "需要携带的数据"
     });
}

private void throwException(){
   // 前面的代码
   throwExceptionSub();
   // 后续代码,假如throwExceptionSub抛出了异常,则不会执行
}

private Object handle(){
    try{
        throwException();
    }catch(ExceptionData ex){
         return { 
              status : ex.getCode() ,
              msg : ex.getMessage() ,
              data : ex.getData()
         }
    }
}