Thursday, February 19, 2015

What are severity and state in RAISERROR implementations and what do they mean back on the C# side?

Following up on this, they are the middlemost and last parameters seen here respectively.

RAISERROR ('Oh no!',11,0)

 
 

This suggests that the later number ("state" for error code reporting) can really be any integer you want it to be between zero and two hundred fifty-five, but that and this clarify that severity, the first number, runs from zero to twenty-five with each step representing an escalated severity with twenty-five being most severe and only settings from eighteen and downwards being specifiable without holding the sysadmin server role! Sixteen is suggested to be the default, and I found that if you use a setting below eleven that it will not even be caught as an exception in C#! I wanted to catch an error (from a RAISERROR) when updating a record with a sproc at the C# side and have it bubble up to a DevExpress control. I ended up writing a try/catch block which had a catch block for a SqlException before a regular catch block for an Exception. If an exception makes it through to the second catch block for Exception, I am logging the error but otherwise swallowing the exception, but if, upstream of that, the SqlException's catch block is entered I am turning around and outright throwing the error like so:

   }
   catch (SqlException exception)
   {
      throw exception;
   }
   catch (Exception exception)
   {
      mylogger.Log(exception);
      return false;
   }
   return true;
}

No comments:

Post a Comment