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

查看:111
本文介绍了C#异常处理(给定一个构造一个字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有一个的ToString类WeekdayException()。
我想返回字符串中构造成立(非法平日:+ wday)用的ToString()。 ?如何访问字符串

 使用系统; 

类WeekdayException:ApplicationException的{
公共WeekdayException(字符串wday):基地(非法平日:+ wday){}

公共重写字符串的ToString()
{
返回HELLO+ ???;
}
}

类TryCatchFinally
{
公共静态无效的主要(字串[] args)
{

{
抛出新WeekdayException(通过试抛出);
}
赶上(ApplicationException的前)
{
Console.WriteLine(抓......+ ex.ToString());
}
}
}

和是这样的(制作和使用的ToString())的是C#程序员使用的方法?如果不是,有什么去了?


解决方案

这是如何访问 wday 特别加入了私人成员变量(见下文):

 类WeekdayException:ApplicationException的{
私人只读字符串工作日;
公共WeekdayException(字符串wday):基地(非法平日:+ wday){
this.weekday = wday;
}

公共重写字符串的ToString()
{
返回HELLO+ this.weekday;
}
}




和这是(制作和使用的ToString())的C#程序员使用的方法?如果不是,有什么去了?




为您设置并使用消息异常通常属性,当你调用构造函数的基 ApplicationException的这是设置。

 类WeekdayException:ApplicationException的{
公共WeekdayException(字符串工作日)
:基地(非法平日:+工作日){}
}

然后:

 尝试{
抛出新WeekdayException(星期二);
}
赶上(WeekdayException weekdayException){
Console.WriteLine(weekdayException.Message);
}



最后,不要像缩写名平日更短的变种,比如 wday 。只需使用全名。


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.

这篇关于C#异常处理(给定一个构造一个字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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