Exception handling

Programmers partition application logic that may throw exceptions into a try block, followed by a catch block to handle these exceptions. An optional finally block, if present, is executed, regardless of whether an exception is thrown by a try block. You cannot just have a try block—it has to be accompanied by either a catch block or a finally block. 

In this section, we will look at different code blocks in order to understand the usage of the try-catch statement, the try-finally statement, and the try-catch-finally statement.

You can use a try-catch statement without a finally block like so:

try
{
//code block which might trigger exceptions
}
catch (SpecificException ex)
{
//exception handling code block

}

The system also allows you to use a try block with a finally block—there's no need for the catch exception. This is shown in the following code:

try
{
// code block which might trigger exceptions
}
finally
{
// Dispose resources here.
//Block you want to execute all times irrespective of try block is executed or not.
}

Last but not least, there's the try-catch-finally block:

try
{
// Code that you expect to throw exceptions goes here.
}
catch (SpecificException ex)
{
// exception handling code block
}
finally
{
// code block that you want to run in all scenarios
}

A compile-time error is thrown if the runtime identifies incorrect syntax in a try block; for example, a try block without a catch or finally block during the compilation of the code. When you don't provide a catch or finally block, the compiler puts a red mark next to the closing bracket of try and an error is thrown, as shown in the error list window in the following screenshot:

Exception filters are a type of exception that's used to catch in a catch block. System.Exception is the base class for any exception type class. As this is the base class, it can hold any exception in the code. We use this when we have code that handles every exception or when we are throwing an exception while calling method().

We've already discussed that a try block can have multiple catch blocks with different exception filters. When the runtime evaluates the catch block, it takes a top-to-bottom approach and executes the most specific catch block that suits the exception that's been caught. If the exception filter in the catch block matches the exception that's been thrown or matches the base class of the exception that's been thrown, it's executed. As an exam tip, always remember to place the most specific catch statements on top and place the generic ones at the bottom.

Understanding the importance of exception handling helps you write proper code that handles every possible scenario and executes it without unexpected behavior occurring. For example, let's say your program is trying to open and write into a file and you receive an exception such as File not found or File-in-Use. Exception handling allows us to handle these scenarios. In the first case, the prompt asks the user to provide a correct filename, while in the second case, the prompt checks whether it is OK to create a new file.

In the following example, a for loop is throwing an index is out of range exception: 

public static void ExceptionTest5()
{
string[] strNumbers = new string[] {"One","Two","Three","Four" };
try
{
for (int i = 0; i <= strNumbers.Length; i++)
{
Console.WriteLine(strNumbers[i]);
}
}
catch (System.IndexOutOfRangeException e)
{
Console.WriteLine("Index is out of range.");
throw new System.ArgumentOutOfRangeException(
"Index is out of range.", e);
}
}

The code handles it and displays a message on the screen before throwing it so that the invoking method can handle it, like so:

However, our main program doesn't handle the exception system. Instead, it uses the default and displays a dialog box:

The finally block releases any variables or objects that were created in the try block. This block executes last and always runs if present:

public static void ExceptionTest6()
{
FileStream inputfile= null;
FileInfo finfo = new FileInfo("Dummyfile.txt");
try
{
inputfile = finfo .OpenWrite();
inputfile.WriteByte(0xH);
}
finally
{
// Check for null because OpenWrite() method might return null.
if (inputfile!= null)
{
inputfile.Close();
}
}
}

In the preceding example, we created a file object in a try block and tried to write some bytes to it. When the runtime completes the execution of the try block, it executes a finally block and releases the file object that was created in the try block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.250