3

I have some code which catches the exception, rolls back the transaction and then rethrow the exception.

catch ( Exception exSys )   {
    bqBusinessQuery.RollBackTransaction();
    throw exSys ;
}

If I use this code, VS Code analysis throws warning saying

Use 'throw' without an argument instead, in order to preserve the stack location where the exception was initially raised.

If I use the code

catch ( Exception exSys )   {
    bqBusinessQuery.RollBackTransaction();
    throw;
}

then I get a warning saying

The variable 'exSys' is declared but never used

How should I solve this problem?

Edit I tried this method, but it doesn't work. system.exception class requires an extra message, along with inner exception. If I do that, it will throw a new message overriding the message from the original exception. I don't want to get the new exception, I want to throw the same exception with same message.

    catch (System.Exception ex)
    {
        throw new System.Exception(ex);
    }

Edit

        catch (System.Exception ex)
        {
            throw new System.Exception("Test",ex);
        }

Tried this method. And then manually caused an exception using throw new Exception("From inside");. Now, ex.Message returns "Test" instead of "From inside". I want to keep that "From inside" message as is. This suggested change will cause problem with error display code everywhere. :/

0

2 Answers 2

9

You do not have to bind a variable to the exception:

try
{
    ...
}
catch (Exception) 
{
    bqBusinessQuery.RollBackTransaction();
    throw;
}

Actually, in your case, as you catch any exception, you do not have to even name the exception type:

try
{
    ...
}
catch
{
    bqBusinessQuery.RollBackTransaction();
    throw;
}

Or (as suggested @Zohar Peled) throw a new exception, using the caught exception as an inner exception. This way you both preserve the stack and give the exception more context.

try
{
    ...
}
catch (Exception e)
{
    throw new Exception("Transaction failed", e);
}

If you actually want to use the exception for some processing (e.g. log it), but want to rethrow it intact, declare the variable, but use a plain throw:

try
{
    ...
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    throw;
}
0
4
catch (Exception)   
{
    bqBusinessQuery.RollBackTransaction();
    throw;
}

If you don't plan on using the exception (e.g. passing the message somewhere) then you don't need to pull it out into a variable. You can simply catch, do custom thing and throw.

11
  • We do in some cases, pass the message. For example, when query validation fails, throw new Exception("custom message"); is common in the application. Then the same is forwarded to the outermost calling function which updates the UI to display error. May 11, 2016 at 8:19
  • In that case you should throw new exception("custom message", originalException) so you don't lose the stack trace
    – Kyle Muir
    May 11, 2016 at 8:21
  • 1
    for the splendid use of pink you get a +1
    – user585968
    May 11, 2016 at 8:21
  • @KyleMuir Can I do throw new exception(originalException). I am not trying to add any new message, just preserving the original message that was there when exception first occurred. E.g. execute query function throws exception saying primary key already exists, then i just want to rollback the transaction and let the same exception message pass through. May 11, 2016 at 8:25
  • 1
    What about: catch (Exception ex) { throw new Exception(ex.Message, ex); }, however, food for thought: if you're throwing a new Exception with all the same information as the original - why are you doing it?
    – Kyle Muir
    May 11, 2016 at 8:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.