Search code examples
javaandroidsqlitesqlexception

why don't I need catch the SQLExecption when I use db.exec() on android?


Why don't I need catch the SQLExecption when I use db.exec() on android?

The db is a SQLiteDatabase object.

when I write the code following, Eclipse don't remind me to catch the SQLException and the code can run normally.However,the method should throw a SQLException according to document provided by eclipse.

I think the programmer should catch every exception or add throws statement when creating a method in java.

The following is my code.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    SQLiteDatabase db=openOrCreateDatabase("keys.db",Context.MODE_PRIVATE,null);
    String sql="CREATE TABLE `mykey` (_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
            "public_key VARCHAR, private_key VARCHAR)";
    db.execSQL(sql);
    db.close();
}

thanks.


Solution

  • Look at the documentation: SQLException extends RuntimeException and you don't need a throws clause for these.

    EDIT: Please note that this applies to Android only. In Java SE, SQLException does not derive from RuntimeException.