枚举“错误级别”的用户定义的异常

时间:2011-02-05 18:05:46

标签: c# exception enums enumeration user-defined

我已经做了一些用户定义的异常,我想为每个异常添加一个错误级别。例如userInputerror和internalError。我编码的原因是因为我想了解异常,这就是为什么我重新发明轮子而不是使用log4net。

我的代码看起来像这样。

我有一个名为MyException的类:

namespace ExceptionHandling
{
    public class MyException : ApplicationException
    {
        public MyException(string message)
            : base(message)
        { 

        }

        public MyException(string message, Exception innerException)
            : base(message, innerException)
        { 

        }
    }
}

我的一个例外是IntParseException,这个类看起来像这样:

namespace ExceptionTester
{
    class IntParseException : MyException
    {
    string dataToParse;

    public IntParseException(string dataToParse)
        : base(" [ERROR] Could not parse " + dataToParse + " to int")
    {
        this.dataToParse = dataToParse;

    }

    public IntParseException(string dataToParse, Exception innerexception) 
        : base(" [ERROR] Could not parse " + dataToParse + " to int", innerexception)
    {
        this.dataToParse = dataToParse;           
    }
}

}

在我的主要表单中,我正在处理我的异常:

    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            try
            {
                int.Parse(txtNumber.Text);
            }
            catch (Exception ex)
            {  
                throw new IntParseException(txtNumber.Text, ex);
            }
        }
        catch (Exception ex)
        {
            LogWriter lw = new LogWriter(ex.Source, ex.TargetSite.ToString(), ex.Message);
            logwindow.upDateLogWindow();
            MessageBox.Show("Please enter a number");                          
        }
    }

我正在记录我的异常并且该类看起来很喜欢这个:

namespace ExceptionTester
{
    class LogWriter
    {
        public LogWriter(string exSource, string exTargetSite, string exMessage)
        {
            StreamWriter SW;
            SW = File.AppendText(@"logfile.txt");
            string timeStamp = "[" + DateTime.Now + "] ";
            string source = "An error occured in the application ";
            string targetSite = ", while executing the method: ";
            SW.WriteLine(timeStamp + source + exSource + targetSite +exTargetSite + exMessage);
            SW.Flush();
            SW.Close();
        }

    }
}

所以我的问题是,如何在我的异常中添加一个枚举类型,这样我就可以选择我想记录的异常类型。例如,在某些时候我想记录所有异常但在另一个时间我只想记录userInputErrors。

3 个答案:

答案 0 :(得分:3)

BTW,Microsoft不再建议您派生自ApplicationException。直接从Exception导出。

此外,在您的日志记录中,请务必记录整个异常。使用ex.ToString(),而不是ex.Message等。这将保证您看到异常类希望您看到的所有内容,包括所有内部异常。

答案 1 :(得分:1)

如果您只是想记录某些异常,为什么不根据抛出的异常类型做出决定,并在您调用它时放弃重新发明轮子?

try
{
    // do stuff that might throw exception
}
catch(ExceptionType1 ex1)
{
    // Ignore
}
catch(ExceptionType2 ex2)
{
    // Log
}

但是,在你的自定义异常中添加一个枚举是微不足道的,尽管我不会强调这个HORSE HOCKEY!

enum ExceptionLevel
{
    One, Two, Three
}

class MyException : ApplicationException
{
    ExceptionLevel ExceptionLevel;
}

答案 2 :(得分:0)

我建议创建一个继承自Application Exception的新基类例外类,其中包括enum:s

public enum ErrorLevel { UserInputError, DatabaseError, OtherError... };

public class BaseException : ApplicationException {
  protected ErrorLevel _errorLevel;
  ...
}

从此基类派生所有异常,并根据需要分配级别 - 可能在构造函数中,或使用从应用程序传递的参数。

相关问题