Visual Studio does not stop with an exception that throws

when i have the 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 can't my CustomException throw and stop execution for debugging in Entry.Main or in the constructor of Class1 (or in my DoSomething method)?

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

The exception settings for Visual Studio are set so that all CLR exceptions are thrown only when used by the user.

+2
source share
2 answers

The first probability exception message means what he says, the first random exception .

In your case, this most likely means that your debugger is configured not to dwell on this type of exception. Since this is a custom exception type, this is the default behavior.

To enable break on first chance exceptions, go to Debug -> Exceptions and select the type of exception that the debugger should disable.

+2
source

A first chance exception means that some method threw an exception. Now your code has the ability to handle this.

It seems that CallToMethodWhichThrowsAnyException already handling a CustomException thrown from somewhere inside, and therefore you will not catch it.

In addition, when you restart, you must wrap the original exception so that the stack trace information is not lost:

  catch (Exception ex) { throw new CustomException(ex.Message, ex); // notice the second argument } 
0
source

Source: https://habr.com/ru/post/948116/


All Articles