Avoid trying / tricks on Android

I am new to Android and I started writing code to execute some queries in the database. When I have to handle exceptions, I don’t know how to do it - from Android I used throws declaration for methods, but it seems that throws not allowed in android? Just try-catch ? I say this because eclipse does not offer me to add a throws ad, for example, when I left the Android environment, I believe this is due to extends Activity . So what is the appropriate way to handle exceptions in android? Surrounding every try-catch clause, my code looks awful, and this is not quite what I want to do.

+6
java android exception-handling
source share
3 answers

If the method you use already throws an exception, you can simply throw the exception as a 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); } } 

Another option is to MyException extend RuntimeException . Then the compiler will not force you to catch it or add it to the method signature. RuntimeExceptions are known as unchecked exceptions, meaning you do not need to verify that they occur with try / catch. Examples of this are NullPointer and ArrayOutOfBounds .

+13
source share

I just wondered about some weird throwing handling in the Android environment and found this old question here. Asker John "began to write code to execute some queries in the database", so perhaps he noticed the same thing as me.

Compiles without errors:

 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 indicates that the signature of the onCreate method cannot be changed by inheriting implementations. Secondly, Zeki correctly indicates the difference between checked and unchecked exceptions.

And now, thirdly, I want to add that a lot of the confusion is caused by SQLException .

The SQLException example above uses the android type android.database.SQLException and inherits java.lang.RuntimeException - this is an unchecked exception! No throw declaration required !!!

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

+4
source share

The reason you cannot β€œadd casts to android via eclipse” is because you are not the one who defines interfaces or superclasses. If you want to add an exception to the method signature (as you say, you do it fine), it must also be added to the interface, and you do not control them, so you cannot change it.

For example, the method

protected void onCreate (Bundle savedInstanceState);

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

protected void onCreate (Bundle savedInstanceState) throws a MyException;

but then it will also need to change where the onCreate function is defined, which is in the Activity class, and this is a class that you cannot change (because it is provided by the android library).

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

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

All Articles