简体   繁体   中英

How to create your own exception

I'm specifically looking for a solution for a console app, however answers for form apps are also welcome. Would you be so kind as to help me with the following problem?

My Problem: I want to create my own Exception that will catch any number from 5 to 9 when a user types one of them.

NOTE: I know I can solve this problem by simply playing with IF ELSE statements but I'm specifically looking for it to be CATCHed as Exception.

What I don't understand: Once the user types in 5 for example, my own created exception catches it - What I don't understand is how to tell my created Exception Class what to catch, what to look for? Where do I type these numbers in my Exception and tell my Exception class that those numbers are exceptions?

If I wasn't clear enough, please let me know, I will try to rephrase myself.

You may benefit from an Exception tutorial .

It sounds like you are trying to do three things.

1 Read a number from a text input field.

2 Determine whether that is a valid number.

3 If the number is invalid, throw an exception.

//Read input
int i = -1;
i = int.TryParse(MyTextField.Text, out i);

if (i >= 5 && i <= 9) 
    throw new ArgumentOutOfRangeException("value", "Value cannot be  between 5 - 9.");

If I understand you correct I think you should try something like:

if (yourNumber >= 5 && yourNumber <= 9)
{
    throw new YourException(..);
}

But also see the comments. Your understanding of exceptions isn't correct.

The below code shows the basic example of a CustomException

class Program
{
    static void Main(string[] args)
    {
        try
        {
            int x = Convert.ToInt32(Console.ReadLine());

            if (x >= 5 && x <= 9)
            {
                CustomException e = new CustomException("Please Eneter Another Number");
                throw e;
            }
        }

        catch (CustomException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

public class CustomException : System.Exception
{
    public CustomException(string txt)
        : base(txt)
    {
    }
}

Exceptions don't just happen. When your code throws an exception, for example if you try to divide by 0 and you get a DivideByZeroException - it means that some code somewhere has to have a line like throw new DivideByZeroException(); . (Admittedly, this is a simplification of the matter.)

So in order to throw the exception you want - you have to test the input and if it's not good then throw an exception.

And assuming you don't mean that you need a custom Exception (eg FiveToNineException ) - you can just use this:

if (i >= 5 && i <= 9)
{
    throw new Exception("5 to 9 Exception");
}

Or:

if (i >= 5 && i <= 9)
{
    Exception e = new Exception("5 to 9 Exception");
    e.Data.Add("The number ", i);
    throw e;
}

EDIT

For a very simple custom exception:

public class FiveToNineException : System.Exception
{
    public FiveToNineException() : base() { }
}

and then you can have:

throw new FiveToNineException();

And:

try {/*Do something*/ }
catch (FiveToNineException ex) { }

For more information see this link for an answer to a question of mine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM