public static SQLException chainException ( android . database . SQLException sqlException ) { if ( sqlThrowable < 0 || sqlThrowable >= 9 ) { try { sqlThrowable = 9 ; //return new SQLException (sqlException); // creating by reflection is significantly slower, but since Exceptions should be unusual // this should not be a performance issue. final Constructor < ? > c = SQLException . class . getDeclaredConstructor ( new Class [ ] { Throwable . class } ) ; return ( SQLException ) c . newInstance ( new Object [ ] { sqlException } ) ; } catch ( Exception e ) { sqlThrowable = 1 ; } } // if the code above worked correctly, then the exception will have been returned. Otherwise, we need // to go through this clause and create a SQLDroidSQLException try { // avoid a direct reference to the sqldroidSQLException so that app > API level 9 do not need that class. final Constructor < ? > c = SQLDroidConnection . class . getClassLoader ( ) . loadClass ( "org.sqldroid.SQLDroidSQLException" ) . getDeclaredConstructor ( new Class [ ] { android . database . SQLException . class } ) ; // SQLDroidSQLException is an instance of (direct subclass of) SQLException, so the cast below is correct although // the instance created will always be a SQLDroidSQLException return ( SQLException ) c . newInstance ( new Object [ ] { sqlException } ) ; } catch ( Exception e ) { return new SQLException ( "Unable to Chain SQLException " + sqlException . getMessage ( ) ) ; } }
This will create and return an exception . For API levels less than 9 this will return a SQLDroidSQLException for later APIs it will return a SQLException .