Windbg - Breaking on specific CLR exception

 

I get this question quite frequently, "I know how to break on all CLR exceptions, but how do I break on a specific exception, e.g. I want to see only Sql Exceptions?"

 It  is actually quite easy to do in WinDbg using the soe (StopOnException) command found in sos.dll

StopOnException [ -derived] [ -create | -create2]

<Exception> <Pseudo-register number>

Causes the debugger to stop when the specified exception is thrown,

but to continue running when other exceptions are thrown.

The -derived option catches the specified exception and every

exception that derives from the specified exception.

 

 So after you load SOS (Check Windbg QuickStart guide for instructions on how to do that) you need to run the following:

0:052> !soe -Create System.Data.SqlClient.SqlException

Breakpoint set

0:052> g

(234c.1b28): CLR exception - code e0434352 (first chance)

(234c.1b28): CLR exception - code e0434352 (first chance)

CLR exception type: System.Net.WebException

    "The remote server returned an error: (304) Not Modified."

(234c.1b28): CLR exception - code e0434352 (first chance)

CLR exception type: System.Net.WebException

    "The remote server returned an error: (304) Not Modified."

(234c.1b28): CLR exception - code e0434352 (first chance)

(234c.1cd8): CLR exception - code e0434352 (first chance)

'System.Data.SqlClient.SqlException hit'

First chance exceptions are reported before any exception handling.

This exception may be expected and handled.

KERNELBASE!RaiseException+0x3d:

000007fe`fdb5cacd 4881c4c8000000 add rsp,0C8h

 

You will notice that the debugger is skipping all exceptions and breaking only on the exception type you are interested in.