Click here to Skip to main content
15,888,279 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Recently I have discovered that Try…Catch also captures exceptions inside event handlers which feels wrong. Here is code:

BASIC
Module Module1
Event ExEvent()

Sub Main()
  AddHandler ExEvent, AddressOf ExEventHandler
  Try
   REM Exception from event handler propagates here
   RaiseEvent ExEvent()
  Catch ex As Exception
   Console.WriteLine(ex.Message)
  End Try

  Console.ReadKey()
End Sub

Private Sub ExEventHandler()
 Throw New NotImplementedException
End Sub

End Module


What I have tried:

Search the Web. Also read documentation.
Posted
Comments
Richard MacCutchan 15-Dec-23 7:10am    
If you use Try/Catch then you are specifically telling the system that you will handle the relevant exception. Why do you think that is wrong?
inariy 15-Dec-23 14:38pm    
Because RaiseEvent does not look like ExecuteFunction. Because errors that stays inside event handlers looks like rational thing. But I was fooled by the words & forget that language hides from me what is really happening.

1 solution

An event is just a function call where you don't control when the call happens. The call, and more specifically, the return address for the call goes on the stack just like any other function call.

If you want the gritty details of how exception handling works, you'll need to know about Windows Structured Exception Handling (SEH), on which .NET exceptions are implemented. Matt Pietrek wrote what is probably the definitive primer on SEH outside of the Microsoft documentation. You can find it here[^].

Once you've read that, Chris Brumme has a really good write-up on the .NET exceptions implementation, here[^].

Oh, and to answer the question "How to ignore exceptions inside event handlers?," you can't.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900