Created
March 17, 2015 06:23
-
-
Save dnasca/e9ab261e9dd48c0800f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
//This program will read from a .txt file | |
/* Exception Handling Basics | |
* | |
* An exception is actually a class that derives from the System.Exception class. | |
* The System.Exception class provides valuable information about the exception. | |
* example: Message: Gets a message that describes the current exception | |
* example: Stack Tracek: Provides the call stack to the line number in the method where the exception occured. | |
* | |
* !!Interview Questions!! | |
* | |
* Q. Why handle exceptions? | |
* A1. To give the end user useful information regarding the unhandled exception | |
* A2. To log important unhandled exception details | |
* | |
* Q. Can you tell me a practical application of inheritence? | |
* (Using the exception class object to explain inheritence is practical) | |
* A. If you want to handle exceptions properly. You will have the most specific exceptions at the top most catch blocks, and the general ones at the bottom most catch blocks. | |
* So that if there is any other exception types, we will know for sure the following catch blocks will handle the exception | |
* | |
* ^^ In other words, specific exceptions will be caught before the base general exception, so specific exception blocks should always be on top of the base exception block. | |
* | |
*/ | |
public class Program | |
{ | |
public static void Main() | |
{ | |
StreamReader streamReader = null; | |
try //the code that can possibly cause an exception will be in the try block | |
{ | |
streamReader = new StreamReader(@"C:\SampleFiles\Data.txt"); | |
Console.WriteLine(streamReader.ReadToEnd()); | |
} | |
catch (FileNotFoundException exception) //inherits from System.Exception class (the base class for all exceptions), the catch handles the exception | |
{ | |
Console.WriteLine("The file {0} does not exist", exception.FileName); | |
} | |
catch (Exception exception) //a parent class reference variable can point to a derived class object | |
{ | |
Console.WriteLine(exception.Message); | |
} | |
finally //it is good practice to always release resource sin the finally block because the finally block is guaranteed to execute. Exception or not. | |
{ | |
if (streamReader != null) | |
{ | |
streamReader.Close(); //this is important, it will release the streamReader content from memory whether it catches exceptions or not. | |
//if we were to have the streamReader.Close(); in the try block, and there was an exception, the streamReader would not release | |
} | |
Console.WriteLine("Finally block reached, streamReader closed."); | |
} | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment