TSQL 2005 try-catch block

Posted on 2007-08-09
Last Modified: 2013-11-05
In this TSQL code, assume that the correct name of "col1" is "date_col"
declare @date_var datetime
begin try
    select @date_var = max(col1) from table1
end try
begin catch
    select 'my error!!!'
end catch

This code returns the error message:
Msg 207, Level 16, State 1, Line 6
Invalid column name 'col1'.

and it does NOT display "my error!!!".

I had assumed that after finding the error in the try block, the catch block would execute and I would see the text "my error!!!". But it appears that the catch block is not being executed. What is wrong with my understanding?

Question by:jcpw
    LVL 6

    Assisted Solution

    You must get past simple syntax errors before that will work
    Try changing your code to
    select @date_var = someVarcharColumnNameWithDataInIt from TableThatExists

    and you'll get your error because it can't cast a string that isn't a date to a datetime type.
    LVL 22

    Expert Comment

    Try this in the catch:
    Select null Errornbr, null Severity, null ErrorLine, 'my error!!!' Msg
    LVL 11

    Accepted Solution

    According to Microsoft:

    A TRY&CATCH construct catches all execution errors that have a severity higher than 10 that do not close the database connection.

    Your exception does not qualify since it's more of a compilation error than an error that happens during execution.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now