无法为重写方法添加throws子句,导致我必须返回null

时间:2015-11-16 10:21:34

标签: java android android-asynctask polymorphism

我目前正在使用AsyncTask并在doInBackground方法中运行一段代码,需要处理某个异常。由于覆盖了doInBackground方法,因此我无法在方法中添加throws子句。我已经插入了一个捕获异常的try-catch方法,但由于我的方法返回一个Summoner对象,我必须包含一个return null;语句,我发现我的代码仍在执行此语句。

我对AsyncTask的体验非常有限,所以如果您需要更多信息或我忽略了某些内容,请随时指出。

public class GetSummonerData extends AsyncTask<String, Void, Summoner>
{
    @Override
    protected void onPreExecute()
    {
        Button button = (Button) findViewById(R.id.btnSearch);
        button.setText("Loading...");
    }

    @Override
    protected Summoner doInBackground(String... asyncParams)
    {
        try
        {
            String summonerName = asyncParams[1];
            RiotApi api = new RiotApi("api-key");
            Map<String, Summoner> summoners = null;

            //The following line of code will call the API 
            summoners = api.getSummonersByName(Region.valueOf(asyncParams[0]), summonerName);
            //stage 1
            return summoners.get(summonerName);
        }
        catch (RiotApiException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Summoner result)
    {
        //stage 2
        startNewIntent(result);
    }
}
public void startNewIntent(Summoner summoner)
{
    Intent intent = new Intent(this, ProfileActivity.class);
    intent.putExtra("summoner", summoner);
    startActivity(intent);
}

在第1阶段,summoners变量包含1个Summoner对象。在阶段2,onPostExecute返回的结果等于null。为什么即使try块中有return语句,也会执行返回null?

3 个答案:

答案 0 :(得分:1)

执行return null的原因是因为在try-catch块期间抛出了异常。这会导致try块的所有剩余执行中止(包括return语句)和要执行的catch块。

一旦catch块退出return null,然后执行,因为执行继续正常。

答案 1 :(得分:1)

试试这个简单的代码:

public class Class1 {
public Class1() {
    super();
}
public String fetchString(int i) {
    try {
        int j = 1/i;
        return "passed";
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public static void main(String[] args) {
    Class1 class1 = new Class1();
    System.out.println(class1.fetchString(1));
    System.out.println(class1.fetchString(0));
  }
}

你会意识到当抛出异常时,它会被捕获,因此返回语句在&#34;尝试&#34;块永远不会被调用/执行。 (当i = 0时,异常在1 / i处抛出)。

您的代码也一样。

关于您的其他观察,您不能向正在实现接口的方法添加任何新异常。请查看以下示例。

public interface myinterface
{
  public void foo();
}

现在考虑一下

public class Ximpl implements myinterface
{
    public void foo() throws IOException
    {
    }
 }

现在,如果客户端有像

这样的代码
 myinterface varx = new Ximpl();
  //he can do that 
  varx.foo(); //without putting it in try catch block. (Remember polymorphism)??

如果你真的想在catch块中抛出异常,请创建一个Runtime Exception实例,不需要声明它。

答案 2 :(得分:0)

您可以使用此技巧,在RuntimeException中包装异常并重新抛出它。喜欢这个

    try {
        Files.createDirectories(Paths.get(""));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

通过这种方式,您不需要返回null。

您甚至可以创建自己的异常版本来扩展RuntimeException。这样您就不需要将其添加到方法签名中,也不会强迫其他人抓住它。就像这样

    public class MyValidationException extends RuntimeException {

    }