Event handlers swallowing exceptions

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

NoToes
Posts: 3
Joined: 11 Feb 2011, 02:25
Location: Melbourne, Australia
Contact:

Event handlers swallowing exceptions

Postby NoToes » 11 Feb 2011, 02:58

Hi there!

I've just started playing around with SFS, looks very nice.

One thing that stung me was that the exceptions thrown from event listeners get swallowed.

So something like this...

Code: Select all

public void OnConnection(BaseEvent e) {      
      print("Connected: " + (string)e.Params["success"] );
      print("Logging in");
      sfs.Send(new LoginRequest("test", "", "SimpleChat") );
}


..left me scratching my head as to why I couldn't connect. It throws an InvalidCastException which never gets reported.

It would be nice if this was let through or logged.


[/code]
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 11 Feb 2011, 04:36

You could (and should) put your own code in a try-catch block and process it there. The reason event callbacks are wrapped in their own master try-catch is that several might get processed during your update loop. You don't want one slightly malformed custom handler to suddenly stop all message processing right?
NoToes
Posts: 3
Joined: 11 Feb 2011, 02:25
Location: Melbourne, Australia
Contact:

Postby NoToes » 13 Feb 2011, 22:59

You're suggesting all my handlers look like this?

Code: Select all

void OnWhatever(BaseEvent e) {
    try {
        ....
    }
    catch( Exception e ) {
        Debug.Log( "Error - " + e.ToString() );
    }
}


That's going to get tedious. If it is the recommended way, then you should document it somewhere, none of your examples do this.

Surely a log message in your master try/catch makes sense here?

Silently swallowing exceptions is not cool. And you want to be cool right? ;)
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 13 Feb 2011, 23:57

Yes, its' tedious. No one said exception handling wasn't.

But be more fine grained than that. If you're doing IO operations, wrap that specific block in an try { ... } catch (IOException) { } finally { }. The reason being, you can take corrective action better then.

As for the log message in the master try / catch, I'll buy that. I just didn't want to see it ever percolate upwards beyond that.

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 40 guests