控制台打印异常,页面显示异常,http获取链接超时异常

来源:互联网 发布:淘宝上买电视可靠吗 编辑:程序博客网 时间:2024/05/21 08:50

工作中,对于异常的抛出讲究的是异常信息是尽量精确的,因此抛出到前台的异常大都是我们自己编写的异常信息。

PostMethod postMethod = new PostMethod(url);String rs;try{RequestEntity entity= new StringRequestEntity(jsonObject.toString(), "application/json", "utf-8");postMethod.setRequestEntity(entity);client.executeMethod(postMethod);//获取返回内容rs = postMethod.getResponseBodyAsString();} catch (Exception e){e.getMessage();e.printStackTrace();throw new BizException(e.getMessage());}

以上面这段代码为例,如果客户端设置了// client.getParams().setConnectionManagerTimeout(5000);//设置链接超时时间
// client.getParams().setSoTimeout(5000);//设置访问超时时间

这样的链接和读取超时异常,启动程序后,如果调用超时,则在会进入到catch中,首先,e.getMessage()获取到异常信息,然后e.printStackTrace()将异常打印到控制台。打印到控制台的目的是将异常显示给开发人员看,让开发人员迅速定位错误原因。然后,throw new bizException()  ,这里是个封装的自定义异常,将异常抛出。抛出后的异常,由使用这个httpClient方法的类获得。如下:

@ResponseBody@RequestMapping(value={"/applyCis5UPMobile","/applyCis5UPMobileLevel","/applyCis5UMobileSup"})public Json applyCis5UPMobile(String appNo,String queryTy,String localOrCis){Json j = Json.newSuccess();try{logger.info("从页面传来的参数是:"+"appNo="+appNo+",queryTy="+queryTy,"localOrCis="+localOrCis);TmUpMobileRiskResp tmUpMobileRiskResp=commonCis5Service.upMobileRisk(appNo,queryTy,Integer.parseInt(localOrCis));logger.info("查询征信后的返回结果是:"+JSONObject.toJSONString(tmUpMobileRiskResp));return this.setErrorMsg(tmUpMobileRiskResp, tmUpMobileRiskResp.getIfsuccess(), localOrCis);}catch(Exception e){e.printStackTrace();j.setFail(e.getMessage());return j;}}

这里是返回到控制器,再由这里的try catch捕获到异常,通过json返回到前台。在前台弹窗体显示e.getMessage()中的异常信息。

try catch一般是开发者认为某处代码可能会异常而加的,所以如果能够锁定异常的原因则会在throw new bizException中写死异常信息。


原创粉丝点击