一个java类的问题
public class ProfitReport {
private HttpServletRequest request=null;
private String begin="";
private String end="";
/**
* <p>Construtor</p>
* @param request 请求对象
* <p>Create Time: 2007-08-04</p>
*/
public ProfitReport(HttpServletRequest request,String Begin ,String End) {
this.request = request;
this.begin=Begin;
this.end=End;
}
EspSys espsys=new EspSys();
YanZheng yz = new YanZheng(request);
ContractMethod cm = new ContractMethod(request);
private String dbname=espsys.getDBName(request);//获取数据库名称
private HashMap ProHashMap=getRecvSumTable(begin, end);现在这里报错,说Unhandled exception type Exception 怎么解决啊
public HashMap getRecvSumTable(String Begin,String End )throws Exception
{
HashMap hm = new HashMap();
Vector vtRecvSum = new Vector();//--收货记录
Vector vtBuyForm = new Vector();//--购进单记录
Vector vtPayForm = new Vector();//--调拨单记录
//-收货记录
// 0 1 2 3 4 5 6 7 8 9 10
String sqlStr=" select id ,sendid,buyid,cityid,saltid,accdton,frecvton,transformton,bagtype_id,spec_id,bagmaterial_id"+
" from syrecvsum where (proisbuy=1 or issend=1) and status=0 ";
//--购进单记录
// 0 1 2 3 4 5 6
String sqlStr1=" select id,sendid,mixedfee,recvids,cityid,saltnames,createdate from sybuyform where createdate>='"+Begin+" 00:00:00'"+
" and createdate<='"+End+" 23:59:59'";
//--调拨单记录
// 0 1 2 3 4 5 6
String sqlStr2=" select id,buyid,fatherid,sumsdate,recvids,suppliers,saltnames from syrecvsums where sumsdate>='"+Begin+" 00:00:00'"+
" and sumsdate<='"+End+" 23:59:59'";
try
{
vtRecvSum=BasePeer.executeQuery(sqlStr);
vtBuyForm=BasePeer.executeQuery(sqlStr1);
vtPayForm=BasePeer.executeQuery(sqlStr2);
}
catch(Exception ex)
{
ex.getMessage();
}
hm.put("RecvSum", vtRecvSum);
hm.put("BuyForm", vtBuyForm);
hm.put("PayForm", vtPayForm);
return hm;
}
』
[解决办法]
有潜在的exception你必须要处理 把该语句放到try块中 并做异常处理
[解决办法]
好像是你的getRecvSumTable方法签名中声明了Exception 异常,你在ProfitReport 类中用getRecvSumTable时得作出处理
换成下面的试试
try{
private HashMap ProHashMap=getRecvSumTable(begin, end);
}catch(Exception e){
//do something;
}
[解决办法]
private HashMap ProHashMap=getRecvSumTable(begin, end);现在这里报错,说Unhandled exception type Exception 怎么解决啊
这句话说明
getRecvSumTable(begin, end); 在使用的时候会发生异常。 而你没有捕获这个异常。所以才会有如此的提示
你可以
- Java code
private HashMap ProHashMap=null; public ProfitReport(HttpServletRequest request,String Begin ,String End) { this.request = request; this.begin=Begin; this.end=End; try{ ProHashMap =getRecvSumTable(begin, end);}catch(Exception e){ .....}}
[解决办法]
代码可以如此更改:
1.更改getRecvSumTable函数,将函数声明抛出异常去掉,在函数里面try,catch,然后将异常转换为RuntimeException后抛出.
2.private HashMap ProHashMap=null,然后在构造函数里调用ProHashMap=getRecvSumTable(begin, end);但你的构造函数得修改了,要处理异常.
对于楼主的代码,有几点提醒注意:
1.private HashMap ProHashMap=getRecvSumTable(begin, end);这样的写法不合适.通常会出现楼主这样的异常无法处理的情况.
2.ProHashMap是变量,首字母应该小写,其他人看你的代码很容易看错.
3.private String dbname=espsys.getDBName(request);
private HashMap ProHashMap=getRecvSumTable(begin, end);
这些代码应该放到构造函数之前.
4.private String begin="";
private String end="";
begin,end声明为null就可以了.
5.sql语句是用加号拼接而成,可能会有sql注入漏洞.
6.sql查询的异常只做了catch,catch后没有做任何处理,要是查询有错误,有可能你自己都不知道问题出在哪里.楼主如果是为了简便,可以把异常转换为RuntimeException再抛出.不至于出了问题而发现不了
