我该处理异常吗? [英] HOw do I handle exceptions

查看:62
本文介绍了我该处理异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义异常无效,请任何人都可以给我一些建议???



My custom exception isn't working, please can anyone give me some advice???

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Validator_Click(object sender, RoutedEventArgs e)
        {

            catchingException test = new catchingException();

            try 
            {
                
                
               //Validating Order
                
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add( "OrderValidator.xsd");
                settings.ValidationType = ValidationType.Schema;


                XmlReader order = XmlReader.Create("order.xml", settings);
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(order);

                ValidationEventHandler handler = new ValidationEventHandler(ValidationEventHandler);

                xdoc.Validate(handler);
               // test.catchException();
                    
            }
         
            catch (databasetypeException val)
            {
               
                MessageBox.Show(val.Message);

            }
        }







[Serializable]
   public class databasetypeException : Exception
   {
       public databasetypeException()
           : base()
       {
       }
       public databasetypeException(string message)
           : base(message)
       {
       }
       public databasetypeException(string message, Exception innerException)
           : base(message, innerException)
       {
       }
       public databasetypeException(SerializationInfo info, StreamingContext context)
           : base(info, context)
       {
       }

       public void catchException()
       {
           Exception format = new Exception("Format is invalid ");
           throw format;
       }
   }
   }

推荐答案

代码,例外情况,没有任何意义所有,这就是原因。首先,您定义了异常类 databasetypeException ,但从不抛出它的实例。方法 catchException 没有意义,它没有捕获任何东西,只抛出一些不相关的类型 System.Exception 的异常。难怪:捕获异常的方法毫无意义,因为无论如何你必须使用一些try-catch块。 (同时,方法处理一些异常确实有意义。)无论如何,你从不称这种方法。



你只有一个try-catch块。最有可能的是,您在本地捕获异常。通常情况下,异常会被极少数,战略性选择的代码部分所捕获;在大多数情况下,你只是让它们从当前的堆栈帧开始(传播),远离投掷它的堆栈帧。但我不能责怪你做错了,因为我知道你正在做一些学习,这很好。



我试图快速解释这个机制(时间机器 )在我过去的答案中:

C#构造函数中的异常会导致来电者分配到失败? [ ^ ] ,

存储位置操作系统中的.net异常 [ ^ ]。



实际上,我们的想法是高度隔离来自特殊过程的正常指令流唱。它可以帮助您在大多数开发周期中忘记大多数特殊情况(包括但不限于错误)。



还有什么?传统上,像您这样的特定于应用程序的异常应该来自 System.ApplicationException ,而不是来自 System.Exception 。这不是一个大问题,只需要整齐的代码设计风格和可支持性。



-SA
The code, exception-wise, makes no sense at all, that's why. First of all, you define the exception class databasetypeException, but never throw an instance of it. The method catchException make no sense, it does not catch anything, only throws some exception of unrelated type System.Exception. No wonder: methods catching exceptions make no sense, because you have to use some try-catch blocks anyway. (At the same time, methods handling some exceptions do make sense.) Anyway, you never call this method.

You have only one try-catch block. Most likely, you catch the exception too locally. Usually, exceptions are caught in very few, strategically chosen parts of code; in most cases, you just let them go (propagate) from the current stack frame, far from the stack frame of throwing it. But I cannot blame you for doing it wrong, because I understand you are doing some study, which is good.

I tried to quickly explain this mechanism ("time machine") in my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

In effect, the idea is to highly isolate "normal" instruction flow from exceptional processing. It helps you to forget about most exceptional cases (including, but not limited to errors) in most of your development cycle.

What else? Traditionally, application-specific exceptions like yours should be derived from System.ApplicationException, not from System.Exception. This is not a big problem, just a matter of neat style of the design of your code, and supportability.

—SA


这里我还在处理我的问题,我的问题是。在我创建了包含自定义消息的自定义异常之后,如何调用或将该异常添加到我的try-catch块中。谢谢大家的帮助

Here I am still dealing with my problem, my question is. after I have created my custom exception which includes a custom message, how do call or add that exception to my try-catch block. thank you everyone for your help
namespace ValidationProgram
{
    [Serializable]
    public class MyException : Exception
    {
        public MyException() { }
        public MyException(string message) : base(message) { }
        public MyException(string message, Exception inner) : base(message, inner) { }
        protected MyException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

}







namespace ValidationProgram
{
    public class testException : MyException
    {
        public override string Message
        {
            get
            {
                return "data type is incorrect";
            }
        }
    }
}


这篇关于我该处理异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆