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 ?

Raed Alsaleh
  • 1,581
  • 9
  • 27
  • 50

3 Answers3

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;
}
Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
  • @RaedAlsaleh You need to add an appropriate constructor to `JOVALException` that calls the base constructor passing the inner exception. See link. – Sebastian Negraszus Feb 04 '15 at 13:14
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);
 }
DGibbs
  • 14,316
  • 7
  • 44
  • 83
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"
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215