Error handling

  • I have created following table for testing purposes:

    CREATE TABLE TESTTABLE

    (

    id int not null,

    txt1 varchar(20) not null,

    int2 int not null

    )

    Example 1. Insert first record

    insert TESTTABLE

    select 1, 'Text1', 1

    if @@ERROR <> 0

    print 'error occured'

    Result: The record is succesfully inserted.

    Example 2. Insert second record:

    insert TESTTABLE

    select 2, 'Text2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2

    if @@ERROR <> 0

    print 'error occured'

    Result = 'error occured' (msg 8152 - string or binary data would be truncated). @@ERROR catches the error.

    Example 3. Insert third record:

    insert TESTTABLE

    select 3, 'Text3', 'Text3'

    if @@ERROR <> 0

    print 'error occured'

    This insertion results in a syntax error (msg 245 - Conversion failed when converting the varchar value 'Text4' to data type int. @@ERROR DOES NOT catch the error.

    4. Example Insert fourth record:

    insert TESTTABLE

    select 4, 'Text4', 4, 'Text4'

    if @@ERROR <> 0

    print 'error occured'

    This insertion results in a syntax error (msg 213 - column name or number of supplied values does not match table definition. @@ERROR DOES NOT catch the error.

    QUESTION:

    Does anyone know how to catch errors in example 3 and 4?

    Thanks

    /Mikkel

  • Generally speaking using try/catch is preferred to examining @@error.

    Using try/catch will successfully trap the runtime errors encountered for items 1, 2 and 3.

    begin try

    insert TESTTABLE

    select 1, 'Text1', 1

    end try

    begin catch

    print 'error occured'

    end catch

    begin try

    insert TESTTABLE

    select 2, 'Text2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2

    end try

    begin catch

    print 'error occured'

    end catch

    begin try

    insert TESTTABLE

    select 3, 'Text3', 'Text3'

    end try

    begin catch

    print 'error occured'

    end catch

    However, the error in #4 is going to throw a compile error. That means it will parse fine but the exception is thrown when the engine compiles your statement. That means it can't be caught because it actually happens prior to executing.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply