【问题标题】:Error: catch parameter is not a pointer to an interface type iOS错误:catch 参数不是指向 iOS 接口类型的指针
【发布时间】:2013-06-07 09:22:25
【问题描述】:

我正在尝试在我的 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 的新类并将其导入。

【问题讨论】:

    标签: ios objective-c exception xcode4.5


    【解决方案1】:

    你需要通过指针来捕捉异常,像这样:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        你可以这样做:

        @try
        {
          [authenticate createDatabase];
        }
        @catch (NSException *exception)
        {                              
            NSLog(@"Exception: %@", exception);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-04
          • 2016-02-25
          • 2012-11-24
          • 1970-01-01
          • 2011-12-18
          • 2016-08-01
          • 2023-01-19
          • 1970-01-01
          相关资源
          最近更新 更多