Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Code Design - Event call, Catch exceptions or not?

Code Design - Event call, Catch exceptions or not?

Scheduled Pinned Locked Moved C#
questioncsharpdatabasewpfdesign
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GerVenson
    wrote on last edited by
    #1

    Hey, i was working on some API for networking when i implemented some sort of logging and Reporting, in certain situations when all of the sudden my Test project in WPF didn't worked as expected. After some debugging i found a Cross-Thread exception that was caused by some bindings. The API is using some High Level of threading and Tasking support. The problem was that i was attached to a Event that was trigger in a Non-Dispatcher Thread, and inside my ViewModel i did not synchronized the code. The Whole application was still running ( AppDomain had some global exception Handling ) but the point is, this external exception ( as seen from the API side ) had noting to do with the API but prevented it from working by throwing an Exception and stops the execution of the code after the Event Trigger. I learned due my time as a Developer that some sort of "General" exception catching should be prevented where possible. This does not mean an empty TryCatch block ( What is actually even very Bad ). So my Question is: Should i break this Design guide and more, should i catch Exceptions in general when i Invoke an event? The event handler could Throw some exceptions what i do not expect and i should not worry about. And last but not least: If jes, what to do when we catch an event?

    L 1 Reply Last reply
    0
    • G GerVenson

      Hey, i was working on some API for networking when i implemented some sort of logging and Reporting, in certain situations when all of the sudden my Test project in WPF didn't worked as expected. After some debugging i found a Cross-Thread exception that was caused by some bindings. The API is using some High Level of threading and Tasking support. The problem was that i was attached to a Event that was trigger in a Non-Dispatcher Thread, and inside my ViewModel i did not synchronized the code. The Whole application was still running ( AppDomain had some global exception Handling ) but the point is, this external exception ( as seen from the API side ) had noting to do with the API but prevented it from working by throwing an Exception and stops the execution of the code after the Event Trigger. I learned due my time as a Developer that some sort of "General" exception catching should be prevented where possible. This does not mean an empty TryCatch block ( What is actually even very Bad ). So my Question is: Should i break this Design guide and more, should i catch Exceptions in general when i Invoke an event? The event handler could Throw some exceptions what i do not expect and i should not worry about. And last but not least: If jes, what to do when we catch an event?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Jean-Pierre Bachmann wrote:

      should i catch Exceptions in general when i Invoke an event?

      Nah. If you did and an exception occurs, how would I even know?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups