我找到两中解决的方法:
1.将bookItems提升为成员变量然后在函数的最后部分,返回。
public ArrayList getBookItemList()
{
.....
return bookItems(最后的位置);
}
1.将bookItems提升为成员变量然后在函数的最后部分,返回。
public ArrayList getBookItemList()
{
.....
return bookItems(最后的位置);
}
解决方案 »
- 求教~~~String转换为byte类型十六进制数,在线等!!!!!!!!!!
- Spring注解注入,应用启动异常:BeanCreationException
- jms消息接收采用queue的方式,网络断了的话,消息接收不能重新监听和获得消息.请高手指点
- JBPM web designer 怎么保存流程?
- 谁能介绍一下Jetspeed-2
- 请教各位高手啊:为什么使用struts时提示:Servlet action is not available
- 如何用程序将resultset 与jdbtable组件绑定????
- 有谁在java中做过事务处理(不是在EJB中)?
- 谁能给我个springmvc的 前台AJAX请求 然后转成JSON格式 然后前台展示数据的例子啊
- 项目重新部署后过一段时间就连接不上
- java中应该如何调用SQL聚合函数max?
- 关于JB9与struts的问题
catch(Exception ex)
{
throw new EJBException(ex);
}
在找到该方法之前,我又碰到了一个麻烦,为什么它不让我这样抛呢?
throw new Exception(ex);
它报告 说:unreported exception java.lang.Exception;must be caught or declared to be thrown
难道不能原封不动的重抛吗?C++还可以的。
ArrayList bookItems = new ArrayList();try {
}
catch (Exception e) {
}return bookItems;
bookItems.add(bookItem);
}
return bookItems;
你的bookItems 到}的时候已经消亡了...你return的时候根本没有这个bookItems了...2要想throw exception,你必须要有 throws ... ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
方法声明处加入throws Exception
public ArrayList getBookItemList() throws Exception
{
.....
}
这样才可以
throw exception
{
try
{ Collection books = bookHome.findAllBooks(); ArrayList bookItems = new ArrayList();
Enumeration items = Collections.enumeration(books);
while(items.hasMoreElements())
{
Book book = (Book)items.nextElement();
System.out.println("enumeration");
BookItem bookItem = new BookItem(book.getIsbn(), book.getName());
bookItems.add(bookItem);
}
return bookItems;
}
catch(Exception ex)
{
System.out.println("Exception in findAllBooks in BookHandlerBean: " + ex);
return null;
}
}
要是发出异常不是没有返回值了吗?所以在 if(){}else{}里面也会出现这种问题
“你的bookItems 到}的时候已经消亡了...你return的时候根本没有这个bookItems了”
你好象看错了,我return的是bookitems不是bookitem,我的bookitems作用域在try内,是可以返回di.至于我没有声明throws Exception是因为我编的是EJB,我在远程接口已经声明抛出了。这里就没必要了。
异常机制和if else的return好象不同的呀,我同意你的看法,if else的每个分支都需要return,但异常好象是不需要的呀。是这样的吗?有没有其他人可以作个裁决呀?