Pregunta

I have a stored procedure that contains the following code:

IF @somevar IS NULL
BEGIN
RAISERROR (N'Station %d does not exist.', -- Message text.
       10, -- Severity,
       7, -- State,
       @id); 
END

I want to catch the SqlException in C# code. But that exception is not raised. What am I doing wrong?

Thanks.

¿Fue útil?

Solución

I believe you need a higher severity level; try it with a severity of at least 11. Also, are you using ExecuteScalar - which seems to eat exceptions? See here.

Otros consejos

I'd try looking at the severity you're using. http://msdn.microsoft.com/en-us/library/ms178592.aspx

Try this: - I use 16 all the time

 RAISERROR ('Message here', 16, 1)

[Details here]

0-9 Informational messages that return status information or report errors that are not severe. The Database Engine does not raise system errors with severities of 0 through 9.

10 Informational messages that return status information or report errors that are not severe. For compatibility reasons, the Database Engine converts severity 10 to severity 0 before returning the error information to the calling application.

11-16 Indicate errors that can be corrected by the user.

20-24 Indicate system problems and are fatal errors, which means that the Database Engine task that is executing a statement or batch is no longer running. The task records information about what occurred and then terminates. In most cases, the application connection to the instance of the Database Engine may also terminate. If this happens, depending on the problem, the application might not be able to reconnect. Error messages in this range can affect all of the processes accessing data in the same database and may indicate that a database or object is damaged. Error messages with a severity level from 19 through 24 are written to the error log.

You can try with THROW instruction

Link : http://msdn.microsoft.com/fr-fr/library/ee677615.aspx

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top