How to teach Exception Handling for New Programmers?
- by Kanini
How do you go about teaching Exception Handling to Programmers. All other things are taught easily - Data Structures, ASP.NET, WinForms, WPF, WCF - you name it, everything can be taught easily.
With Exception Handling, teaching them try-catch-finally is just the syntactic nature of Exception Handling.
What should be taught however is - What part of your code do you put in the try block? What do you do in the catch block?
Let me illustrate it with an example.
You are working on a Windows Forms Project (a small utility) and you have designed it as below with 3 different projects.
UILayer
BusinessLayer
DataLayer
If an Exception (let us say of loading an XDocument throws an exception) is raised at DataLayer (the UILayer calls BusinessLayer which in turns calls the DataLayer), do you just do the following
//In DataLayer
try
{
XDocument xd_XmlDocument = XDocument.Load("systems.xml");
}
catch(Exception ex)
{
throw ex;
}
which gets thrown again in the BusinessLayer and which is caught in UILayer where I write it to the log file?
Is this how you go about Exception Handling?