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. UILayerBusinessLayerDataLayerIf 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 DataLayertry {    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?
How to teach Exception Handling for New Programmers?
java;programming practices;c#;teaching;exceptions
To explain exception handling, explain the concept behind it: The code where an error occurs frequently does not know how to properly handle that error.  The code that knows how to handle it properly could be the function that called that one, or it could be further up the call stack.When you write a routine that calls a routine that might throw an exception, if you know how to handle that error correctly, put the call in a try block and put the error-handling code in the catch block.  If not, leave it alone and let something above you in the call stack handle the error.Saying catch ex, throw ex is not a good way to do exception handling, since it doesn't actually handle anything.  Plus, depending on how the exception model in your language works, that can actually be harmful if it clears stack trace information that you could have used to debug the issue.  Just let the exception propagate up the call stack until it hits a routine that knows how to handle it.