How to Rethrow an Exception

104 31
    • 1). Create the "try/catch" statement. The use of "try" is to execute code until an error occurs. Once an error is found, the next statement executed in the code is the "catch" phrase. This gives the programmer the ability to handle errors in the software. The code below is a shell to set up a "try/catch" block.
      try
      {
      }
      catch
      {
      }

    • 2). Add the application code to the "try" block. For this example, a function is called that causes an error. When the function errors, the compiler jumps to the code held within the "catch" block. Anything after the function in the "try" block will not execute.
      try
      {
      ThisFunctionWillError();
      }
      catch
      {
      }

    • 3). Add the exception class to the "catch" block. An instance of the exception class needs to be created to use its methods and properties. Below, the code instantiates the class and uses the variable "ex." This naming scheme is standard practice.
      try
      {
      ThisFunctionWillError();
      }
      catch (Exception ex)
      {
      }

    • 4). Rethrow the exception. Once the error is cleaned up, rethrow the exception to return control to the normal application code flow.
      try
      {
      ThisFunctionWillError();
      }
      catch (Exception ex)
      {
      CleanUpMyError();
      throw;
      }

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.