0
  catch (Exception ex)
  {
     _errorCode = ErrorCodes.SqlGeneralError;
     CommonTools.vAddToLog(ex, _errorCode, _userID);
     throw new JOVALException(_errorCode);
 }

I use this peace of code to handle error to an custom exception called (JOVALException) but when an exception is occurred it is open "No source available" page and telled me that is no the stack trace is empty How could I solve this problem ?

Edit

 public JOVALException(ErrorCodes _errCode, String _message = "")
        {
            ErrMessage = _message;
            ErrCode = _errCode;

        }

here is my constructor, How can modify it ?

3 Answers 3

5

Put ex into your JOVALException as an inner exception.

public JOVALException(ErrorCodes _errCode, String _message = "",
  Exception innerException = null) : base(_message, innerException)
{
  ErrMessage = _message;
  ErrCode = _errCode;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@RaedAlsaleh You need to add an appropriate constructor to JOVALException that calls the base constructor passing the inner exception. See link.
0

I'd recommend you to modify JOVALException class, by adding a constructor:

  public class JOVALException: Exception {
    ...
    // Note Exception "innerException" argument
    public JOVALException(ErrorCodes _errCode, Exception innerException, String _message = "")
      : base(_message, inner) {
      ...
    }
  }

Another issue: try avoid code duplication

  ErrMessage = _message;

since ErrMessage is, in fact, a duplication of Message propepery; just call a base class constructor.

then

  catch (Exception ex)
  {
     _errorCode = ErrorCodes.SqlGeneralError;
     CommonTools.vAddToLog(ex, _errorCode, _userID);
     throw new JOVALException(_errorCode, ex);  // <- Note "ex"
  }

Comments

0

When you call throw new JOVALException(_errorCode); you lose the stack trace of the original error.

Just do:

throw;

If you need to throw a JOVALException you'll need to modify your class slightly:

class JOVALException : Exception
{
    public JOVALException(string errorCode, Exception innerException)
        : base(errorCode, innerException)
    {

    }
}

And an example:

 try
 {
      int i = 0;
      int foo = i / i;
 }
 catch (Exception ex)
 {
     _errorCode = ErrorCodes.SqlGeneralError;
     CommonTools.vAddToLog(ex, _errorCode, _userID);
     throw new JOVALException(_errorCode, ex);
 }

1 Comment

but I need to tell caller method that is "JOVALException" !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.