如何覆盖Java中的类方法并添加"throws"(抛出)?申报到吗? [英] How do I override a class method in Java and add a "throws" declaration to it?

查看:134
本文介绍了如何覆盖Java中的类方法并添加"throws"(抛出)?申报到吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中有没有可能抛出"东西的AsyncTask?如果我不@Override方法,则不会调用该方法.如果我在末尾添加"throw",则有编译器错误.例如,我想做类似的事情:

Would it be at all possible to have an AsyncTask in Android which "Throws" things? If I don't @Override the method, it doesn't get called. If I add the "throws" at the end, there are compiler errors. For example, I want to do something like:

class testThrows extends AsyncTask<String,Void,JSONObject> {
   @Override
   protected JSONTokener doInBackground(<String>... arguments) throws JSONException {
      String jsonString = arguments[0];
      JSONTokener json = new JSONTokener(jsonString);
      JSONObject object = json.getJSONObject("test");
      return object;
   }
}

json.getJSONObject抛出JSONException.有什么办法吗?

json.getJSONObject throws a JSONException. Is there any way to do this?

推荐答案

您不能执行此操作.多亏了Time4Tea,我得以将回调方法添加到发生错误时被调用的类中.例如:

You cannot do this. Thanks to Time4Tea, I was able to add callback methods to the class that get called when there is an error. For example:

try {
   JSONTokener json = new JSONTokener(stringToJsonify);
   JSONObject object = json.getJSONObject("test");
} catch (JSONException e){
   onJsonException();
}

其中onJsonException是类回调方法.

Where onJsonException is the class callback method.

这篇关于如何覆盖Java中的类方法并添加"throws"(抛出)?申报到吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆