C# 在子类中处理异常

C# 在子类中处理异常,c#,exception-handling,subclass,C#,Exception Handling,Subclass,我的问题是: 如何在connect()方法(传播)中处理异常 我应该使用这个关键字(this.Open()…或Open()就足够了) 给定的基类: class BaseClient { public BaseClient(string host, string port); public void Open(); public bool Authenticate(); public void Close(); } 要创建对象的实例并建立连接,请执行以下

我的问题是:

  • 如何在connect()方法(传播)中处理异常
  • 我应该使用这个关键字(this.Open()…或Open()就足够了)
  • 给定的基类:

    class BaseClient
    {
        public BaseClient(string host, string port);
        public void Open();
        public bool Authenticate();
        public void Close();
    }     
    
    要创建对象的实例并建立连接,请执行以下操作:

    BaseClient client = new BaseClient(host, port);
    
    try
    {
        client.Open();
        bool flag = !client.Authenticate();
        if (flag)
        {
            throw new SNException();
        }
    }
    catch (Exception ex)
    {
        bool flag = client.State == State.Opened;
        if (flag)
        {
            client.Close();
        }
    }
    
    现在,我希望将BaseClient子类化,并将Open()和Authenticate()方法封装在一个方法connect()中

    其中:

    public void connect()
    {
        this.Open();
        ...
        this.Authenticate();
        ...
    }
    
    public void disconnect()
    {
        this.Close();
    }
    
    1.如何在connect()方法(传播)中处理异常

    如果您没有任何方法来处理
    connect
    中的错误,例如捕获错误并以静默方式返回布尔值
    false
    ,请不要执行任何操作-保持代码原样

  • 我应该使用这个关键字(this.Open()…或Open()就足够了)
  • 如果所使用的方法标识符与其他定义的方法不一致,则不需要使用
    例如,使用NamespaceWithAuthenticate;
    会破坏此功能)。使用此选项来解析方法是隐含的。

    1。如何在connect()方法(传播)中处理异常? 枚举类型

    public enum State {OK, WARNING, ERROR, OPENED, CLOSED};
    
    初始化Exception类的新实例

    internal class SNException : Exception
    {
        private State _state;
    
        public State GetState
        {
            get { return this._state; }
        }
    
        public SNException()
        {
        }
    
        public SNException(State state)
        {
            this._state = state;
        }
    
        public SNException(string message)
            : base(message)
        {
        }
    
        public SNException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    
        protected SNException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
            : base(info, context)
        {
        }
    }
    
    基本客户端类

    class BaseClient
    {   
        private string host = "";
        private string port = "";
    
        public BaseClient(string host, string port)
        {
            this.host = host;
            this.port = port;
        }
    
        public void Open()
        {
            throw new SNException(State.CLOSED);
        }
    
        public bool Authenticate()
        {
            return false;
        }
    
        public void Close()
        {}
    }     
    
    class ChildClient : BaseClient
    {
        public ChildClient(string host, string port) : base(host, port)
        {
        }
    
        public bool connect()
        {
            this.Open();
    
            return this.Authenticate();
        }
    
        public void disconnect()
        {
            this.Close();
        }
    }
    
    类ChildClient InHirtis位于基BaseClient类上

    class BaseClient
    {   
        private string host = "";
        private string port = "";
    
        public BaseClient(string host, string port)
        {
            this.host = host;
            this.port = port;
        }
    
        public void Open()
        {
            throw new SNException(State.CLOSED);
        }
    
        public bool Authenticate()
        {
            return false;
        }
    
        public void Close()
        {}
    }     
    
    class ChildClient : BaseClient
    {
        public ChildClient(string host, string port) : base(host, port)
        {
        }
    
        public bool connect()
        {
            this.Open();
    
            return this.Authenticate();
        }
    
        public void disconnect()
        {
            this.Close();
        }
    }
    
    调用对象

    ChildClient childClietn = new ChildClient("host", "port");
    try
    {
        bool result = childClietn.connect();
    }
    catch (SNException ex)
    {
        var st = ex.GetState;
    }
    finally
    {
        childClietn.disconnect();
    }
    
    2.我应该使用这个关键字(this.Open()…或Open()就足够了)? this关键字引用类的当前实例,并且还用作扩展方法的第一个参数的修饰符

        public BaseClient(string host, string port)
        {
            this.host = host;
            this.port = port;
        }
    

    如果方法或构造函数中的此参数与对象中的变量具有相同的名称,则需要明确指示在何处赋值。

    对于第二个问题
    Open()
    应该足够了。对于异常部分,如果
    Connect()
    正在调用
    Authenticate())
    那么,如果未经授权,调用方的工作就是传播异常。请参阅,其中的共识是“不,他们不应该”!