Exception handling in C#
You can handle exceptions in C# using try, catch, and finally code blocks. Use this strategy when you need to generate custom error codes for your application. It's best for situations where the user doesn't have full control over the input (like a file operation). This is a "forward" manner of handling exceptions so it doesn't provide the ability to retry what caused the exception. You can take an exception object as a parameter for catch to access properties like [C#] .Message. You can use multiple catch blocks with different exception objects for different kinds of exceptions, but be sure to finish with the generic one (Exception e) to ensure exceptions are always caught.

try { //statements } catch(Exception e) { //taking an exception object is optional //statements } finally { //statements }

The finally block is always executed regardless of whether or not an exception is caught. Use this block to close open files, resources, etc. to avoid dangling pointers, memory leaks, etc.
pending review
Posted 2015-01-01

Courtney {1}
4 snippets
Joined 2015-01-01
Hello! I'm from Toronto and programming is what I do. I've used C, PHP, C#, Java, JavaScript, and a few scripting and pseudo programming languages. Lately I've been learning the power of regex. I created Peerfectionist to be a user-centric code sharing site that promotes quality and pride in one's work. I will continue to work hard developing this site over the coming years.

Log in to comment!