Answer the question
In order to leave comments, you need to log in
Passing an exception to the parent form. How to do without crutches?
Form with two buttons. When the first button is clicked, the child button is called via ShowDialog wrapped in a try-catch. When the second button is clicked, the exception should be passed to the parent form, but this does not always happen. Sometimes the second form catches an unhandled exception. How to win without creating crutches in the form of refusing to throw Exception
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
try
{
Form1 a = new Form1();
a.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void simpleButton2_Click(object sender, EventArgs e)
{
Close();
throw new NullReferenceException("test");
}
}
}
Answer the question
In order to leave comments, you need to log in
In general, you can leave it as you have it. It's easy to catch only exceptions to the logic of YOUR program.
The rest can be left at the mercy of global handlers:
https://msdn.microsoft.com/en-us/library/system.ap...
https://stackoverflow.com/a/406473
Personally, I don’t like the option I proposed, because .to. a generic message interceptor is the last thing to go for, the last resort.
In my opinion, it is better to work out the logic. in order to reduce the possibility of exceptions, switch to events - i.e. inside your handler class has a try..catch that calls the event on error.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question