1

我正在尝试在我的 Xcode 项目中使用异常处理。

- (void)viewDidLoad
{
    [super viewDidLoad];

    authenticate = [[UseDb alloc]init];
    @try{
    [authenticate createDatabase];
    }
    @catch (DatabaseCreationException) {
        NSLog(@"failed to create database");
    }
}

这是引发异常的方法:

-(void)createDatabase
{

    NSString *docsDir;
    NSArray *dirPaths;
    char *sql_stmt1,*sql_stmt2,*sql_stmt3,*sql_stmt4;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    _mDatabasePathDb = [[NSString alloc]
                     initWithString: [docsDir stringByAppendingPathComponent:
                                      @"SmartDiaryDatabaseTest2.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: _mDatabasePathDb ] == NO)
    {
        const char *dbpath = [_mDatabasePathDb UTF8String];

        if (sqlite3_open(dbpath, &_mDb) == SQLITE_OK)
        {

            char *errMsg;

            sql_stmt1 = "CREATE TABLE IF NOT EXISTS USERDETAIL (mUserName TEXT PRIMARY KEY, mPassword TEXT,mUname TEXT, mImage TEXT, mGender CHAR, mDob DATE, mEmail TEXT, mAddress TEXT, mMaritalStatus BOOL, mLatitude DOUBLE, mLongitude DOUBLE)";

            if (sqlite3_exec(_mDb, sql_stmt1, NULL, NULL, &errMsg) != SQLITE_OK)
            {

            }

            sql_stmt2 = "CREATE TABLE IF NOT EXISTS CONTACTS (mName TEXT, mContactno TEXT, mCgender CHAR, mCdob DATE, mCemail TEXT, mClatitude DOUBLE, mClongitude DOUBLE)";

            if (sqlite3_exec(_mDb, sql_stmt2, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"failed to create table");

            }

            sql_stmt3 = "CREATE TABLE IF NOT EXISTS TODO (mStartDate DATE, mEndDate DATE, mTaskName TEXT, mTaskDescription TEXT, mPriority INT)";

            if (sqlite3_exec(_mDb, sql_stmt3, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"failed to create table");
            }


            sql_stmt4 = "CREATE TABLE IF NOT EXISTS FAVORITES (mTdate DATE, mNewsLabel TEXT )";

            if (sqlite3_exec(_mDb, sql_stmt4, NULL, NULL, &errMsg) != SQLITE_OK)
            {

            }
           // sqlite3_close(_mDb);

        } else {
            NSLog(@"failed to create database");
            NSException *e = [DatabaseCreationException exceptionWithName:@"DatabaseCreationFailedException" reason:@"sqlite execution failed" userInfo:nil];
            @throw e;

        }

    }
    sqlite3_close(_mDb);
}

我收到了问题中提到的错误:catch parameter is not a pointer to an interface type iOS我第一次在 Objective C 中处理异常。请告诉我哪里出错了。我创建了一个名为 DatabaseCreationException 的新类并将其导入。

4

3 回答 3

2

您需要通过指针捕获异常,如下所示:

@try{
    [authenticate createDatabase];
}
@catch (DatabaseCreationException*ex) {
//                               ^
    NSLog(@"failed to create database");
}

请注意,Apple 强烈建议不要使用异常来处理运行时情况,将它们保留用于应用程序即将关闭的情况(即不可恢复的情况)。如果情况可以恢复,Apple 建议使用NSErrorExceptions Programming Guide)。

于 2013-06-07T09:24:58.160 回答
0

使用指向异常对象的指针:

@catch (DatabaseCreationException *e) {
        NSLog(@"failed to create database");
}

但是我认为这种方法无论如何都是错误的;如果抛出异常,您似乎正在泄漏数据库句柄。我认为你应该坚持返回BOOL更有效并避免泄漏。

于 2013-06-07T09:25:12.793 回答
0

你可以这样做:

@try
{
  [authenticate createDatabase];
}
@catch (NSException *exception)
{                              
    NSLog(@"Exception: %@", exception);
}
于 2013-06-07T09:33:57.723 回答