2018-10-01 异常处理(捕捉异常)try--catch--finally

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31726339/article/details/82917437

处理异常的语法结构如下


try
{

}

catch(<exceptionType> e)
{

}

finally
{
}

其中catch块可以有0个或多个,finally可以有0个或1个,但是如果没有catch块,必须有finally块,没有finally块,必须有catch块,catch块和finally块可以同时存在。

 

try块包含了可能出现异常的代码(一条或者多条语句)

catch块用来捕捉异常,当代码发生异常,那么异常的类型和catch块中的类型一样的时候,就会执行catch块,如果catch块的参数不写,表示发生任何异常都执行这个catch块。

展开阅读全文

异常捕捉异常处理嵌套及引发异常

04-09

<p>rn &nbsp;rn <p>rn 请务必到<a href="https://edu.csdn.net/bundled/detail/49?utm_source=banner">https://edu.csdn.net/bundled/detail/49?utm_source=banner</a>&nbsp;下单购买课,方可添加图书寄送地址。rn </p>rn <p>rn 本页面,仅为观看视频页面,如需邮寄图书,请务必到<a href="https://edu.csdn.net/bundled/detail/49?utm_source=banner">https://edu.csdn.net/bundled/detail/49?utm_source=banner</a>&nbsp;下单购买课rn </p>rn <div>rn <br />rn </div>rn</p>rn<p>rn 《21天通关Python》我能收获哪些?rn</p>rn<p>rn <br />rn</p>rn<ul class=" list-paddingleft-2" style="font-family:-apple-system-font, BlinkMacSystemFont, &quot;font-size:17px;text-align:justify;">rn <li style="color:#333333;">rn <p>rn <span style="font-size:14px;">1<span style="color:#333333;font-family:-apple-system-font, BlinkMacSystemFont, &quot;font-size:14px;">本</span>实体图书教材,京东畅销书《疯狂<span>Python</span>讲义》含邮寄</span> rn </p>rn </li>rn <li>rn <p>rn <span style="font-size:14px;"><span>10</span><span style="color:#333333;font-family:-apple-system-font, BlinkMacSystemFont, &quot;font-size:14px;">场</span><span>Python视频精讲,作者李刚亲自带队</span><span style="color:#333333;">带你通关Python</span></span> rn </p>rn </li>rn <li style="color:#333333;">rn <p>rn <span style="font-size:14px;">4<span style="color:#333333;font-family:-apple-system-font, BlinkMacSystemFont, &quot;font-size:14px;">大</span>模块,涉及数据分析、网络爬虫、等Python热点领域</span> rn </p>rn </li>rn <li style="color:#333333;">rn <p>rn <span style="font-size:14px;">专属微信社群,随时答疑,爱上Python!</span> rn </p>rn </li>rn</ul>rn<p>rn <span><span style="font-size:14px;"><br />rn</span></span> rn</p>rn<br />rn<p>rn <br />rn</p>rn<p>rn <span></span> rn</p>

struts2异常处理捕捉不到局部异常

07-07

试用struts2的异常处理机制rn配置文件 struts.xmlrn[code=XML]rnrn /error.jsprn rn rnrn rn rn rn rnrn....rnrn rn /fileNotFound.jsprn rnrn[/code]rnrnrnaction中代码为:rn[code=Java]rn//下载其他数据文件rn public InputStream getData() throws Exceptionrn InputStream inStream = null;rn rn HttpServletRequest request = ServletActionContext.getRequest();rn String fdataid = request.getParameter("fdataid");rn String type = request.getParameter("type");rn// System.out.println("g"+type+"2");rn String dataname = request.getParameter("fdataname");rn// request.setCharacterEncoding("UTF-8");rn//此处故意重新编译,获得乱码的datanamern dataname=new String(dataname.getBytes("ISO-8859-1"),"UTF-8");rnrn// System.out.println(dataname+"!!!!!!!!!!!!!!!!!!!2");rn if(fdataid!=null)rn tryrn rn this.tbhead= munuservice.getHeadTableByDataid(Integer.valueOf(fdataid));rn String location=this.tbhead.getFLocation(); rn GetProperties gp=new GetProperties();rn //读取文件根路径的位置rn String fileRoot=gp.getValue("StoreNextConfig.properties", "FileRoot");rn String file=fileRoot+location+"\\"+dataname; rn rn inStream=new FileInputStream(file);rn rn dataname=dataname.replace("\\", "/");rn //显示的文件名rn String fileData[]=dataname.split("/");rn rn if(fileData.length>0)rn rn this.fileName= fileData[fileData.length-1]; rn elsern rn this.fileName=dataname; rn rn rn catch(FileNotFoundException fnex)rn fnex.printStackTrace();rn System.out.println("系统找不到文件:"+dataname);rn throw new FileNotFoundException("系统找不到文件:"+dataname);rn rn rn rn elsern rn throw new FileNotFoundException("ID:"+fdataid+"对应的文件不存在!");rn rn rn return inStream;rn rn[/code]rnrnrn系统运行后能够输出,后台输出为:rn[code=Java]java.io.FileNotFoundException: Z:\corestore\store3\G\G01\G0101\G010101\G010101003\水体指数\???-WI.tfw (文件名、目录名或卷标语法不正确。)rn at java.io.FileInputStream.open(Native Method)rn at java.io.FileInputStream.(FileInputStream.java:106)rn ...rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:619)rn系统找不到文件:???-WI.tfwrn2010-07-07 10:47:58,968 [org.apache.struts2.dispatcher.StreamResult]-[ERROR] Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the tag specified for this action.rn[/code]rnrn但是在客户端跳转的异常处理界面是error.jsprn在error.jsp中输出exception.message,获得总是Can not find a java.io.InputStream with the name [data] in the invocation stack. Check the tag specified for this action. rn rn谁能给解释一下,分数不够可以再加!rn 论坛

异常捕捉

06-23

需求:在catch的时候记录异常。rn问题描述:现在我做的是在finally的时候通过ls_errmsg串来记录,但是发现问题在finally写的时候会出现问题,如插入,删除的时候无响应。如果在各个catch后面都记录的话这样同样的代码要重复多次,希望能得到指点。rnrn[code=Java]rnpublic ActionForward doSave(Action action, HttpServletRequest request,HttpServletResponse response) throws Throwable rn request.setCharacterEncoding("UTF-8");rn response.setCharacterEncoding("UTF-8");rn response.setContentType("text/html; charset=UTF-8");rn String policemanid= (String)request.getSession().getAttribute(Constants.ACC_LOGINNAME);rn Hashtable requestData = WebUtil.getRequestData(request);rn requestData.put("policemanid", policemanid);rn String ls_errmsg = null, ls_ret = null;rn try rn AddressBusiness business = (AddressBusiness)BasBusinessFactoryrn .getBusiness(REG_CODE);rn business.doSave(requestData);rn RecordSysOperate.setLog(requestData,"保存", Constants.NORMAL_POLICE, "保存核查地址", "普通民警保存核查地点");rn catch (BusinessException e) rn ls_errmsg = e.getMessage();rn catch (DaoException e) rn ls_errmsg = e.getMessage();rn catch (Exception e) rn ls_errmsg = "失败";rn finally rn// RecordSysOperate.setErrorLog(requestData,"保存", "保存核查地址",ls_errmsg);rn ls_ret = "\"success\":" + (ls_errmsg == null ? "true" : "false")rn + ",\"errmsg\":\"" + (ls_errmsg == null ? "" : ls_errmsg)rn + "\"";rn rn PrintWriter out = response.getWriter();rn out.print(ls_ret);rn out.close();rn return null;rn rnrn[/code] 论坛

没有更多推荐了,返回首页