Search code examples
c#exceptioncustom-exceptions

exception (handling) in c#


  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 ?


Solution

  • 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"
      }