C#异常处理(用一个字符串给构造函数)。[英] C# exception handling (with a string given to a constructor)

本文是小编为大家收集整理的关于C#异常处理(用一个字符串给构造函数)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我有一个WeekDayException类,其中有ToString(). 我想返回用toString()返回构造函数("非法工作日:" + wday)中设置的字符串.如何访问字符串?

using System;

class WeekdayException : ApplicationException {
    public WeekdayException(String wday) : base("Illegal weekday: " + wday) {}

    public override string ToString()
    {
        return "HELLO" + ???;
    }
}

class TryCatchFinally 
{
    public static void Main(String[] args) 
    {
        try
        {
            throw new WeekdayException("thrown by try");
        }
        catch (ApplicationException ex) 
        {
            Console.WriteLine("Catch ..." + ex.ToString());
        }
    }
}

这(制作和使用toString())是C#程序员使用的方法吗?如果没有,该怎么办?

推荐答案

这是您可以通过添加private成员变量(但请参见下文)的方式访问wday的方式:

class WeekdayException : ApplicationException {
    private readonly string weekday;
    public WeekdayException(String wday) : base("Illegal weekday: " + wday) {
        this.weekday = wday;
    }

    public override string ToString()
    {
        return "HELLO " + this.weekday;
    }
}

这(制作和使用ToString())是C#程序员使用的方法吗?如果没有,该怎么办?

通常,对于您设置并使用Message属性的例外,该属性是在调用base ApplicationException的构造函数时设置的.

class WeekdayException : ApplicationException {
    public WeekdayException(string weekday)
        : base("Illegal weekday: " + weekday) { }
}

然后:

try {
    throw new WeekdayException("Tuesday");
}
catch(WeekdayException weekdayException) {
    Console.WriteLine(weekdayException.Message);
}

最后,不要像weekday这样的缩写为wday之类的较短变体.只需使用全名即可.

其他推荐答案

ApplicationException(string)构造函数设置's Message属性,因此您也应该能够使用Message.

其他推荐答案

我建议您override Message属性:

class WeekdayException : ApplicationException
{
    private readonly string _message;

    public override string Message
    {
        get { return _message; }
    }

    public WeekdayException(String wday)
    {
        _message = "Illegal weekday: " + wday;
    }

    public override string ToString()
    {
        return Message;
    }
}

本文地址:https://www.itbaoku.cn/post/408565.html

问题描述

I have a WeekdayException class that has the ToString(). I want to return the string set up in Constructor ("Illegal weekday: " + wday) with ToString(). How to access the string?

using System;

class WeekdayException : ApplicationException {
    public WeekdayException(String wday) : base("Illegal weekday: " + wday) {}

    public override string ToString()
    {
        return "HELLO" + ???;
    }
}

class TryCatchFinally 
{
    public static void Main(String[] args) 
    {
        try
        {
            throw new WeekdayException("thrown by try");
        }
        catch (ApplicationException ex) 
        {
            Console.WriteLine("Catch ..." + ex.ToString());
        }
    }
}

And is this (making and using ToString()) the method that C# programmers use? If not, what's the way to go?

推荐答案

This is how you can access wday specifically by adding a private member variable (but see below):

class WeekdayException : ApplicationException {
    private readonly string weekday;
    public WeekdayException(String wday) : base("Illegal weekday: " + wday) {
        this.weekday = wday;
    }

    public override string ToString()
    {
        return "HELLO " + this.weekday;
    }
}

And is this (making and using ToString()) the method that C# programmers use? If not, what's the way to go?

Typically for exceptions you set and use the Message property which is set when you invoke the constructor for the base ApplicationException.

class WeekdayException : ApplicationException {
    public WeekdayException(string weekday)
        : base("Illegal weekday: " + weekday) { }
}

Then:

try {
    throw new WeekdayException("Tuesday");
}
catch(WeekdayException weekdayException) {
    Console.WriteLine(weekdayException.Message);
}

Finally, don't abbreviate names like weekday to shorter variants like wday. Just use the full name.

其他推荐答案

The ApplicationException(string) constructor sets the ApplicationException's Message property, so you should be able to use Message as well.

其他推荐答案

I recommend you override the Message property:

class WeekdayException : ApplicationException
{
    private readonly string _message;

    public override string Message
    {
        get { return _message; }
    }

    public WeekdayException(String wday)
    {
        _message = "Illegal weekday: " + wday;
    }

    public override string ToString()
    {
        return Message;
    }
}