Use try/catch to handle exceptions
The purpose of the try-catch block is to catch and process exceptions generated by working code. Some exceptions can be handled in the catch block and no exception is thrown again after the problem is resolved; but in more cases the only thing you can do is make sure that the appropriate exception is thrown.
Example
In this example, IndexOutOfRangeException is not the most appropriate exception: for this method, ArgumentOutOfRangeException is more appropriate, because the error is caused by the index parameter passed by the caller.
class TestTryCatch { static int GetInt(int[] array, int index) { try { return array[index]; } catch ( e) // CS0168 { (); // Set IndexOutOfRangeException to the new exception's InnerException. throw new ("index parameter is out of range.", e); } } }
Comments
The code that causes the exception is enclosed in the try block. Next, add a catch statement to process IndexOutOfRangeException when it occurs. The catch block handles the IndexOutOfRangeException and throws a more appropriate ArgumentOutOfRangeException exception. To provide the caller with as much information as possible, consider specifying the original exception as the InnerException of the new exception. Because the InnerException property is read-only, it must be assigned a value in the constructor of the new exception.
Use finally to execute the cleanup code
The purpose of the finally statement is to ensure that necessary objects (usually objects that hold external resources) can be cleaned immediately even when an exception is raised. An example of such a cleanup feature is to call Close to FileStream immediately after use, rather than waiting for the common language to run the object, as shown below:
static void CodeWithoutCleanup() { file = null; fileInfo = new ("C:\\"); file = (); (0xF); (); }
In order to convert the above code into a try-catch-finally statement, the cleanup code needs to be separated from the working code, as shown below.
static void CodeWithCleanup() { file = null; fileInfo = null; try { fileInfo = new ("C:\\"); file = (); (0xF); } catch( e) { (); } finally { if (file != null) { (); } } }
Because an exception may occur at any time in the try block before the OpenWrite() call, and the OpenWrite() call itself may fail, so we cannot guarantee that the file is open when we try to close it. The finally block adds a check to ensure that the FileStream object is not null before calling the Close method. Without a null check, the finally block may throw its own NullReferenceException, but exceptions should be avoided in the finally block as much as possible.
Close the database connection in the finally block is another good choice. Because sometimes the number of connections allowed by the database server is limited, the database connection should be closed as soon as possible. In situations where the connection cannot be closed due to an exception being raised, using finally blocks is also a better option than waiting for garbage collection.