当前位置: 代码迷 >> J2SE >> 错误如何抓不住?
  详细解决方案

错误如何抓不住?

热度:279   发布时间:2016-04-24 17:09:40.0
异常怎么抓不住??????
代码如下
int   columnPerPage   =   20;
try{
columnPerPage   =   ((Integer)request.getSession().getAttribute( "columnPerPage ")).intValue();
}catch(Exception   ex){
columnPerPage   =   20;
//System.out.println(ex);
}

心想:如果request.getSession().getAttribute( "columnPerPage ")为空那么就要抛出异常,通过catch语句块捕捉异常,给columnPerPage   初值20,那么程序可以继续运行而不退出

事实是,调试发现,当程序运行完columnPerPage   =   20;一句后就直接退出了并在控制台打印一大堆异常:
(主要异常信息:)
16:03:05,484   WARN     [RequestProcessor]   Unhandled   Exception   thrown:   class   java.lang.NullPointerException
16:03:05,500   ERROR   [Engine]   StandardWrapperValve[action]:   Servlet.service()   for   servlet   action   threw   exception
java.lang.NullPointerException

------解决方案--------------------
你确定你这段出错吗
------解决方案--------------------
这段代码的外层是不是还有其他方法?都有异常捕获吗?
------解决方案--------------------
up
------解决方案--------------------
确定这个出错,就来个finally好了
------解决方案--------------------
一定要处理,就来个finally{}
------解决方案--------------------
楼上正解
------解决方案--------------------
同意楼上的方法
  相关解决方案