English 中文(简体)
Java / Cohen: • 如何按返回类型开展工作?
原标题:Java / Android: How to work with return type List<MyType> ?

我的问题是贾瓦·安纳特:

我创建了一个类别<代码>MyClass,其方法为:getAllData(:List<MyType> = 新的ArrayList<MyType>。 其他类别<代码> 我呼吁采用这一方法,并希望将返回名单写到另一个<代码>List<MyType>。

But I get the following error: Unhandled exception type Exception

我可以做些什么?

该法典:

<>MyClass.java

 public List<MyType> datas = new ArrayList<MyType>();

public List<MyType> getAllData() throws Exception{

//add some things to datas... 

return datas;
}

<>My OtherClass.java

 public void fetchData(){

    MyClass mydatas = new MyClass();
    List<MyType> thedatas = mydatas.getAllData();

 }

How can I solve the problem? With an "try / catch(Exception e)" surrounding the statement, it seems not to get the returned List from getAllData();

预祝福!

最佳回答

getAllData(>might<>em> > > 交存一项经核实的例外(即不是的例外情况:RuntimeException) 以来,你必须用<条码>逐条填写。

public void fetchData() {
    MyClass mydatas = new MyClass();
    try {
        List<MyType> thedatas = mydatas.getAllData();
    } catch (Exception ex) {
        // display or log exception name
    }
}

或加入<条码> 方法名称后的条款。

public void fetchData() throws Exception {
    MyClass mydatas = new MyClass();
    List<MyType> thedatas = mydatas.getAllData();
}
问题回答

页: 1 汇编者现在请你在出现例外情形时做些什么。

您应当将“NEVER” throw成“Exception作为方法签名的一部分。 这过于笼统。 相反,你只能试图放弃可能出现的具体例外。

如果你不希望<代码>fetchData>处理例外情况,你必须宣布<代码>fetchData,放弃相同的例外情形:getAllData<>> >,在出现例外情况时,可取得相应的价值。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...