1

I have a windows Form and i'm using C# for this program. In my form i have a button and a textbox.

I want to click on the button and delete the numbers/letters by incorporating a custom exception (for learning purposes), but i keep on getting an error.

The error in my program is when there is nothing in the textbox, and if i click on the delete button the program crashes. Can someone help me out with this?

    public class deleteData : Exception
    {

        public deleteData()
            : base("") {  } 
    }
    private void btn_Delete(object sender, EventArgs e)
    {
        if (textbox1.Text != null)
        {
            textbox1.Text = textbox1.Text.Remove(textbox1.Text.Length - 1, 1);

        }
        else
        {
            throw new deleteData();
        }

    }
9
  • Please use try catch statement in your program and debug the code. Aug 9, 2015 at 4:59
  • 2
    You're throwing an exception if the value is null, it's totally expectable that it will crash. Maybe you want to take some other action instead?
    – Alejandro
    Aug 9, 2015 at 5:03
  • How can still delete the data in the textbox using my custom exception? @user4221591
    – user5072189
    Aug 9, 2015 at 5:08
  • I'm trying to use a custom exception when i delete data on button click, is there a way i could do this? @Alejandro
    – user5072189
    Aug 9, 2015 at 5:12
  • 1
    Throwing an exception for this is a bad design. If there is no data to delete, simply do nothing (or give the user a message, if you want). Throwing an exception when the situation could be handled by the code goes against the whole idea of exceptions - which are for exceptional events.
    – Tim
    Aug 9, 2015 at 5:22

2 Answers 2

1

I modified your program a bit. Give it a try and see if this is what you had in mind. The custom exception pops up a messageBox as its exception handling, and the catch block puts the stack trace in the textBox. You could use either, both, or neither method for handling the exception. The main thing is that as long as the exception occurs in the Try-Catch block and is handled in some way there, you will not crash your program. It handles the exception and keeps on running.

You would not usually do this, but I can see the educational benefit in it.

using System;
using System.Windows.Forms;

namespace TryCatch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "")
                {
                    textBox1.Text = "";
                }
                else
                {
                    throw new deleteData("Here we are having the custom exception do its own exception handling");
                }
            }
            catch (Exception ex)
            {
                textBox1.Text = "Here we are catching the custom exception in a catch block\r\n\r\n";
                textBox1.Text += "Exception details:" + ex.StackTrace.ToString();
            }
        }

        public class deleteData : Exception
        {
            public deleteData(string s)
            {
                MessageBox.Show(s);
            }
        }
    }
}
1
  • I understand that using exception like this is not usually done, but i just wanted to see if it could be done. It was for educational purposes only. @WDS
    – user5072189
    Aug 9, 2015 at 6:12
0

As many people suggested, this is not a good idea, but if you insist, I'll explain how to do this.

You want to check whether text in a text box is null. If it is, then throw an exception. However, I think you misunderstand what it means by "a string is null" and "a string is empty". An empty string is "" and a null string is just null, nothing. A null string cant do anything, if you call its methods, NullRefereneException! But you can call methods from an empty string.Normally the text in text boxes is empty when you see an empty text box. I guess you want to check for that.

if (textbox1.Text != "") {
    textbox1.Text = ""; // This is to remove the text, as you said in the question
} else {
    throw new deleteData ();
}
3
  • 1
    If OP doesn't catch that exception, it will still crash the program.
    – Tim
    Aug 9, 2015 at 5:42
  • But thats what he wants, right? @Tim He doesn't want the program to throw a IndexOutOfRangeException. He wants a deletData exception
    – Sweeper
    Aug 9, 2015 at 5:43
  • 1
    "The error in my program is when there is nothing in the textbox, and if i click on the delete button the program crashes. Can someone help me out with this?" Your current code will not help OP with the program crashing. It will still crash unless the exception is handled at some level.
    – Tim
    Aug 9, 2015 at 5:44

Your Answer

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