0

My application needs to catch and display the exception that has come from back end in the form of an exception object to front end. I need to propagate that error as a parameter to the other classes and set flags and perform some required cleanup and then finally throw the exception. I dont want to lose any stack trace and other information. I see I cant just use throw e. I cant use just throw either as I have got the exception object as a parameter. Could some one help me to throw the received object the right way?

public void FunctionThatGetsExceptionAsparameter(Exception ex)
    {
        //The backend caught the exception, wrapped it and put in an object and then passed it to this function as a parameter 
        //calls function in other class by passing this object
        FunctionInOtherClass(ex);
    }

    public void FunctionInOtherClass(Exception ex)
    {
        if (ex != null)
        {
            //set some flags to perform some other BL because of exception happened in backend
            //send the object to higher layer class
            //calls function in higher layer class by passing this object
            FunctionInHigherLayerClass(ex);
        }

    }

    public void FunctionInHigherLayerClass(Exception ex)
    {
        //Throw the exception object to be caught by our application exception handler to display the standard exception box
        throw ex;
    }
Murthy
  • 177
  • 2
  • 12
  • 1
    Refer to this post: https://stackoverflow.com/questions/57383/how-to-rethrow-innerexception-without-losing-stack-trace-in-c – Bijay Koirala Feb 12 '19 at 10:19
  • This has the wrong smell for me. Why doesn't the code that caught the exception and called `FunctionThatGetsExceptionAsParameter` just rethrow after the function call returns? – 500 - Internal Server Error Feb 12 '19 at 10:29

0 Answers0