Charanoglu
Charanoglu

Reputation: 1339

How to throw exception only in debug?

Suppose I want throw exception only when I use the debug mode, I did:

try
{
   throw new Exception("test)";
}
catch(Exception ex)
{
#if DEBUG
            throw;
#else
Console.WriteLine("exception => " + ex.Message);
#endif
}

this works only on throw, on the else condition I get:

The variable ex is declared but never used

Upvotes: 0

Views: 378

Answers (1)

Dour High Arch
Dour High Arch

Reputation: 21722

#if DEBUG
catch(Exception)
{
     throw;
}
#else
catch(Exception ex)
{
    Console.WriteLine("exception => " + ex.Message);
}
#endif

Upvotes: 2

Related Questions