Trippy Dot Net

Slide Notes Here: http://ow.ly/2qFmJ


2nd seminar in a month c-c-combo! I'm getting my old confidence back :D I'll try to schedule another one next next tuesday (which I will post here too) about Unit Testing~ But in the meantime, here's a few choice bits about my take on Exception Handling (Part 1 of my 3P.Net series):

Be wary of using Try..Catch Exception Handling! Its tempting to use the infamous "Try All, Catch Everything" mentality throughout your code, but that will cause a lot of trouble to you (and your team) in the future. How? Well, aside from a slight performance hit for every line in the try..catch (less than 1ms), by catching exceptions in the lower layers, you're depriving the upper layers from effectively resolving those errors!

An example: If the end user somehow bypasses the validation, and enter a null entry into a non-nullable field, the business logic layer would pass the wrong data to the service layer, who would then try to insert/update the data source. The data source would then issue an exception to the service layer (NullArgumentException). If you resolve the error at the service layer (Log the error then throw a message saying that there's something wrong) then the business logic layer will no longer catch the exception. However! If you let it 'bubble up' (eg. Don't try..catch it, or throw it again after catching it) to the business logic layer, you can now catch all NullArgumentExceptions, fill the null data with a default value, then resend it to the data source. The end user would'nt even know that an exception had happened.

Another thing, it's good practice to throw unresolved exceptions up towards the highest layer, which is commonly the Windows Forms layer (or ASP.Net/WPF/Silverlight layer, whichever the case). The top most layer however, should have a blanket try catch on its containers (form/page/usercontrol, whichever the case) in order to catch any stray unresolved errors that manage to bubble up to the very top. These unresolved errors, are the ones that we have to track with a Bug Tracker.

You can read more about this topics on my slides above, or you could just message me in my Google Profile, Facebook or Twitter. Until next time, sayonara~

0 comments:

Post a Comment