Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Can't catch DB exception

Hey, why I can't catch a DB exception? here's my code:

var Barrel = {} || Barrel;

Barrel.DB = function () {
    this.db = null;
    this.dbname = null;
};

Barrel.DB.prototype.open = function () {
    if ( null === this.dbname ) {
        throw "dbname can not be empty";
    }

    try {
        if ( null === this.db ) {
            this.db = Titanium.Database.open( this.dbname );
        }
    } catch ( e ) {
        throw e;
    }
}

Barrel.DB.prototype.insert = function ( table, data ) {
    // some code ...
    try {
        this.open();
        this.db.execute( query, values );

        var id = this.db.lastInsertRowId;
        Ti.API.debug( "id: " + id );
    } catch ( e ) {
        Ti.API.debug( e ); // never printed
        Ti.API.debug( "n: " + e.name ); // never printed
        Ti.API.debug( "m: " + e.message ); // never printed
        throw e; // never fired
    }

    return id;
}

function Product() {
    this.dbname = "barrel.db";
}

Product.prototype = new Barrel.DB();
Product.prototype.constructor = Product;

var product = new Product();
product.insert( "notices", { col1: "woot", col2: "woot2", col3: null, col4: "null" } );

The exception on the TRACE log is something like this:

[TRACE] I/Database( 413): sqlite returned: error code = 1, msg = table notification has no column named productId
[TRACE] E/TiDB ( 413): (kroll$2) [28,312889] Error executing sql: table notification has no column named productId: , while compiling: INSERT INTO notification (productId, name, price, image, users, url) VALUES (?, ?, ?, ?, ?, ?)
[TRACE] E/TiDB ( 413): android.database.sqlite.SQLiteException: table notification has no column named productId: , while compiling: INSERT INTO notification (productId, name, price, image, users, url) VALUES (?, ?, ?, ?, ?, ?)

So, why I can't catch the exception? the exception is thrown, isn't just catch it?

Thanks.

— asked October 3rd 2010 by Juan Felipe Alvarez Saldarriaga
  • android
  • database
  • exception
  • mobile
1 Comment
  • I too can't catch this (1.4.1.1 & 1.4.2 OSX)

    [ERROR] Error occurred calling next on a PLSqliteResultSet. SQLite error: 'constraint failed' for 'INSERT …..
    [ERROR] The application has crashed with an unhandled exception. Stack trace:

    The table has a unique key and that's why it's failing to inset. But shouldn't this be handled?

    — commented October 14th 2010 by Swaroop Hegde

2 Answers

  • Have you tried a newer Continuous Integration Build? we made a bug fix to correctly throw more exceptions back to the JavaScript context.

    — answered October 4th 2010 by Don Thorp
    permalink
    3 Comments
    • I'm using 1.4.2 from Continuous Integration Build for windows :|.

      — commented October 4th 2010 by Juan Felipe Alvarez Saldarriaga
    • I'm using 1.7.2 from Continuous Integration Build and it also does not catch any SQLite errors. Easy to replace, simply wrap an invalid execute statement in a try {} catch {} and on iOS the app just exists immediately.

      — commented July 20th 2011 by Matthew ORiordan
    • BTW. It seems there is a bug in JIRA for this at http://jira.appcelerator.org/browse/TIMOB-1857?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

      — commented July 20th 2011 by Matthew ORiordan
  • I am also looking to catch DB exceptions, but it doesn't work with SDK 1.7.1, too…..now 1.7.2 is out, but I cannot upgrade my app yet…

    To confirm by others, and especially Don or Paul, I've read in some posts that the Database exceptions cannot be caught in the Simulator, but that they are "working" and can be caught when installed in devices…

    Can this be confirmed ??

    Thank you in advance !

    — answered July 22nd 2011 by abbdoul ab
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.