How to catch all exceptions in a user control

I have a user control that contains several methods, and each of these methods has a try-catch block. I catch only one type of exception, for example ArgumentException. Update. I do not see an exception, except for a few exceptions that I defined.

Each of these methods handles an ArgumentException in the same way, so I have redundant code.

Can I catch an ArgumentException in one place to use all methods in a user element?

Update1: The application is used in production, and it is used by many people who are not very familiar with computers. Simplified - they have to insert a lot of numbers into the database (using a hand-held optical scanner), and, of course, they make mistakes. There are at least 8 common exceptions (wrong size, wrong type, ...), and in my business logic I catch all these exceptions and throw them into the GUI. Since the environment in which the application is installed is very disgusting, I play a loud error sound and display an error message.

Because of the business logic, they have several options for inserting these numbers, so the same error detection logic is found in several methods throughout the user control.

Update2: in fact, I will not catch an ArgumentException, but my own exceptions like CodeFormatException, CodeDoesntExistException, CodeNotInTheSelectedRollException etc.

+3
source share
3 answers

You can get this piece of code into some validation function and call it at the beginning of your function.

+2
source

, WndProc UserControl ( , , UserControl ), , , WndProc, try... catch . try... catch, catch , catch, .

: , , - . , , UserControl:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.Load += new TryCatchHandler(new EventHandler(this.Form1_Load)).EventHandler;
        this.MouseClick += new TryCatchHandler(new MouseEventHandler(this.Form1_MouseClick)).EventHandler;

        this.button1.Click += new TryCatchHandler(new EventHandler(this.button1_Click)).EventHandler;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        // ...
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // ...
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // ...
    }

    private class TryCatchHandler
    {
        public Delegate handler;

        public TryCatchHandler(Delegate handler)
        {
            this.handler = handler;
        }

        public void EventHandler(object sender, EventArgs e)
        {
            try
            {
                this.handler.Method.Invoke(this.handler.Target, new object[] { sender, e });
            }
            catch (ArgumentException exc)
            {
                // ...
            }
        }
    }
}
+1

Derive the exceptions from the base class, and then reformat your exception handling code to a method that takes the base exception class as a parameter. You can then handle the exception and speak up as needed.

0
source

Source: https://habr.com/ru/post/1772284/


All Articles