Structured Exceptions in ADO.NET 2.0/Yukon
Well, tonight I just pretty much finished up the first chapter of one of the two books I'm working on . Anyway, one of the Chapters I'm wriitng is on the T-SQL Enhancements and I got to wondering. How are Exceptions that you raise in T-SQL handled from the client side. My first guess was that “It depends” but that Print Statements would be treated just as they are currently.... Ok, here's the test. First the procedure:
ALTER PROCEDURE dbo.usp_TestProc
AS BEGIN TRY
DECLARE @Value DATETIME
SET @Value = 'cuckoo'
PRINT'Cuckoo, Cuckoo'
END TRY
BEGIN CATCH
Print 'Catch Cuckoo Exception' END CATCH
RETURN
As you can see, I'm intentionally causing an exception condition by converting the literal 'cuckoo' to a DateTime. Now for the code:
SqlConnection cn = null;
private void Form1_Load(object sender, EventArgs e)
{
cn = new SqlConnection(@"Data Source=adamlaptop\cuckoobird;Initial Catalog=Cuckooz;Integrated Security=SSPI;" );
cn.InfoMessage += new SqlInfoMessageEventHandler(MessageHandler);
cn.Open();
SqlCommand cmd = new SqlCommand("usp_TestProc", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
cn.Close();
}
private void MessageHandler(object sender, SqlInfoMessageEventArgs args)
{
MessageBox.Show(args.Message)
};
And just as expected, 'Catch Cuckoo Exception'..........