Page 1 of 1

Event handlers swallowing exceptions

Posted: 11 Feb 2011, 02:58
by NoToes
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]

Posted: 11 Feb 2011, 04:36
by tchen
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?

Posted: 13 Feb 2011, 22:59
by NoToes
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? ;)

Posted: 13 Feb 2011, 23:57
by tchen
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.