How to handle multiple errors

When I run something like

BACKUP LOG [somedb] 
TO DISK = N'i:\log.bak';

It gives 2 error messages:

Msg 3201, Level 16, State 1, Line 2
Cannot open backup device 'i:\log.bak'. Operating system error 3(The system cannot find    the path specified.).
Msg 3013, Level 16, State 1, Line 2
BACKUP LOG is terminating abnormally.

When I try to handle the error using TRY CATCH, the error returned is always 3013. This is a problem for me because I want to know if the backup failed due to out of space or if the disk is missing, etc.

Using @@ ERROR returns the same error number.

Is there a way to handle multiple error messages like these?

+3
source share
1 answer

You need to check the collection Errorsinside SqlException:

catch(SqlException sqlEx)
{
    foreach(SqlError error in sqlEx.Errors)
    {
       int code = error.Number;
       string msg = error.Message;
    }
 }

You should get all the error with all the relevant data in SqlException.Errors

+4
source

All Articles