1

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?

2
  • Why override ToString? Isn't the parent's version going to do what you want? Otherwise, call some of your own (inherited) methods to get the data, or use an instance variable Commented Dec 17, 2010 at 19:55
  • Inheriting from ApplicationException has been dis-recommended for quite some time. Inherit directly from Exception. Commented Dec 17, 2010 at 20:05

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.