0

I need to make it possible for the users to send me back possible exception messages, which I show by wrapping the main method into try-catch, but that also forces me to see that custom message instead of visual studio's exception window.

That's why I need to make use of some preprocessing directive somehow to run the unwrapped method when in VS and wrapped when not.

user1306322
  • 8,561
  • 18
  • 61
  • 122

3 Answers3

2
main(){
try {
// your app code here
} 
catch (Exception ex){
if(System.Diagnostics.Debugger.IsAttached)
{
    throw;
  } else {
  // your exception handling here.
  }
}
Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
  • I'll fix the formatting up a bit later, but this should let you get normal exception handling (without losing the original exception stack trace), while having most exceptions in the field caught by this exception handler. – Tetsujin no Oni Oct 10 '12 at 22:21
  • I think with this I'm actually losing the stack trace. Exception points directly to `throw`. – user1306322 Oct 10 '12 at 22:22
  • look at the exception details. throw by itself doesn't recreate the exception. (and you don't want to throw ex; just throw;) – Tetsujin no Oni Oct 10 '12 at 22:23
  • So I can't actually debug as usual with this? So far in-studio and standalone application behaves as intended, except the stack trace. – user1306322 Oct 10 '12 at 22:25
  • Take a look over here at http://stackoverflow.com/questions/881473/why-catch-and-rethrow-exception-in-c – Tetsujin no Oni Oct 10 '12 at 22:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17836/discussion-between-tetsujin-no-oni-and-user1306322) – Tetsujin no Oni Oct 10 '12 at 22:41
0

As Tetsujin no Oni suggested, use System.Diagnostics.Debugger.IsAttached If this boolean attribute returns true, then the Visual Studio IDE (or some other debugger!) is attached to the process, in which case - assume you're running under Visual Studio.

M.A. Hanin
  • 8,044
  • 33
  • 51
0

No need to use preprocessing directives — it can be done by checking for attached debugger in an if statement:

static void Main(string[] args)
{
    if (System.Diagnostics.Debugger.IsAttached)
    {
        /* application code */
    }
    else
        try
        {
            /* application code */
        }
        catch (Exception ex)
        {                
            // custom exception handling
        }
}
user1306322
  • 8,561
  • 18
  • 61
  • 122