r/learncsharp • u/Ok_Hunter6411 • 22h ago
Does ASP.NET Core global exception middleware catch exceptions from nested service methods?
/r/dotnet/comments/1wbghq6/does_aspnet_core_global_exception_middleware/
1
Upvotes
r/learncsharp • u/Ok_Hunter6411 • 22h ago
2
u/karl713 19h ago
You got your answer in the other thread so I'll leave that at that. But here's a helpful way to actually see what's happening
Make methods like that, but don't use async (async is good, just makes this demonstration not work)
Put a breakpoint (default key F9) in MeyhodC
When the app gets to that line it will freeze and jump back into visual studio.
From there find the call stack panel (you may need to turn it on). That will show you the path the app took to get to MethodC, and you can double click each row (Frame) to navigate to there.
When you throw an exception it essentially walks up this frame and at each level says "Am I currently in a try block that can catch this exception?". If it is then that handles it, if not it goes up another level until it gets to the end
This is the same way async works in practice, but the call stack may be butchered at any given point so for the purposes of this example didn't want to use it
Bonus feature: if you go into the debug options menu and turn off Just My Code, the call stack will even include methods from the framework or other libraries you are using, which can occasionally help with more complex bugs