2

when I have following code:

public class Entry
{
    public void Main()
    {
        var p = new Class1();
    }
}

public class Class1
{
    public Class1()
    {
        DoSomething();
    }

    private void DoSomething()
    {
        try
        {
            CallToMethodWhichThrowsAnyException()
        }
        catch (Exception ex)
        {
            throw new CustomException(ex.Message); // where CustomException is simple System.Exception inherited class
        }
    }
}

Why does my CustomException not get thrown and stop the execution for debugging in Entry.Main or in the constructor of Class1 (or in my DoSomething method)?

There is only the message A first chance exception of type 'MyLibrary.CustomException' occurred in MyLibrary.dll in the immediate window.

Exception settings for Visual Studio are set that all CLR Exceptions are only thrown when User-unhandled.

Felix C
  • 1,755
  • 5
  • 26
  • 40
  • 1
    `CallToMethodWhichThrowsAnyException` is already handling `NullReferenceException` – Zdeslav Vojkovic Mar 14 '13 at 13:26
  • Yes, but in the catch block I'm throwing a new Exception and I'm expecting that my programm will crash on any time. But the exception is just "swallowed" and the executing will continue. – Felix C Mar 14 '13 at 13:27
  • Sorry, I've updated the question.. the message in the Immediate Window is displaying my CustomException, not the concrete exception thrown in `CallToMethodWhichThrowsAnyException`. – Felix C Mar 14 '13 at 13:29
  • @Grant in step by step debugging, the exception dialog is coming up two times (one time for the exception of `CallToMethodWhichThrowsAnyException` and one time for my `CustomException` - but the program continues with execution. – Felix C Mar 14 '13 at 13:30
  • Is the exact code you posted throwing the `CustomException` for you? Do you have any `try{...}catch{...}` in your `CallToMethodWhichThrowsAnyException` or `Main` methods? – Alex Filipovici Mar 14 '13 at 15:13
  • The code above is the exact representation of my try/catch handlers. `CallToMethodWhichThrowsAnyException` is a .NET method which connects to hardware via `SerialPort` (on very basic level) – Felix C Mar 18 '13 at 10:51

2 Answers2

3

First chance exception message means exactly what it says, a First chance exception.

In your case it most probably means that you have your debugger configured not to stop on this type of exception. Since this is a custom exception type, that is the default behavior.

To enable break on first chance exceptions go to Debug -> Exceptions and choose the type of exception you want the debugger to break on.

TheBoyan
  • 6,802
  • 3
  • 45
  • 61
  • +1 for posting the blog post on msdn.. But if I understand it correctly, in the demo code above there should be a second chance exception also being thrown, or not? Because I'm not handling the `CustomException` – Felix C Mar 14 '13 at 13:43
  • @FelixC - That again depends on your debugger configuration – TheBoyan Mar 14 '13 at 13:46
0

The A first chance exception means that some method has thrown an exception. Now your code has a chance to handle it.

It seems that CallToMethodWhichThrowsAnyException is already handling CustomException thrown from somewhere inside it, and this is why you don't catch it.

Besides, when rethrowing, you should wrap the original exception, so that stack trace info is not lost:

    catch (Exception ex)
    {
        throw new CustomException(ex.Message, ex); // notice the second argument
    }
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • I've updated my question. The `CallToMethodWhichThrowsAnyException` is definitily not handling the exception by itself. Besides: Thanks, in my real application I've safed the stack trace by wrapping the application already, in my question it is just demo code. – Felix C Mar 14 '13 at 13:32