Avoid try/catch on Android

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:51:52

问题


I am new in Android environment and I have started writing some code to execute some queries on a database. When I have to handle exceptions I don't know what the appropriate way is to do it - out of Android I used to use throws declaration on methods but it seems that throws isn't allowed in android? Just try-catch? I say this because eclipse doesn't suggest me adding throws declaration like when I am out of Android environment, I guess that it is related to extends Activity. So what is the appropriate way to handle exceptions in android? Surrounding every sentence with try-catch makes my code look terrible and that's not really what I want to do.


回答1:


If the method you are using already throws an exception, you may want to just re-throw the exception as the new type:

public void someMethod() throws IOException {
    try {
        //  Do database operation
    } catch (MyException e){
        throw new IOException(e.toString());
    }
}

//  Or, if there is no exception, use an unchecked exception:

public void otherMethod() {
    try {
        // DB operation
    } catch (MyException e){
        throw new RuntimeException(e);
    }
}

The other option is to make MyException extend RuntimeException. Then the compiler won't force you to catch it or add it to the method signature. RuntimeExceptions are known as unchecked exceptions meaning you don't have to check for them occurring by way of a try/catch. Examples of these are NullPointer and ArrayOutOfBounds.




回答2:


I just was wondering about some strange handling of "throws" in Android environment and found this old question here. Asker Jon "started writing some code to execute some querys on a database", so maybe he noticed the same as I did.

This compiles without error:

public void onCreate(SQLiteDatabase db)
{
    db.execSQL(DbMeta.T_DISGUISED.T_CREATE);
}

Despite this declaration (in javadoc popup):

void android.database.sqlite.SQLiteDatabase.execSQL(String sql) throws SQLException

So first, monkjack is right when he points out that onCreate method's signature cannot be changed by inheriting implementations. And second, Zeki is correctly indicating the difference between checked and unchecked exceptions.

And now third, I want to add that the big confusion is caused by SQLException.

The SQLException used in the example above is Android type android.database.SQLException and inherits java.lang.RuntimeException - it is an unchecked exception! No throws declaration required!!!

That is not the classic java.sql.SQLException - which is a java.lang.Exception and requires try/catch/throws.




回答3:


The reason you can't "add the throws in android via eclipse" is because you are not the person who defines the interfaces or super classes. If you want to add an exception to the method signature (like you say you do normally) it also needs to be added to the interface and you are not in control of them so you can't change it.

Eg the method

protected void onCreate(Bundle savedInstanceState);

which you override in activity, if you want to throw an exception the method signature would need to be changed to (for example)

protected void onCreate(Bundle savedInstanceState) throws MyException;

but then it would also need to change where onCreate is defined, which is in the Activity class - and that is a class you can't change (because its provided by the android library).

Therefore your only option is to catch the Exception and do something with it (or just ignore it). You could make a toast to display the errror

catch (Exception e) {
  Toast toast = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
  toast.show();
}


来源:https://stackoverflow.com/questions/4575736/avoid-try-catch-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!