Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I can't understand why the try-catch block in one dialog box are not caught exceptions (in project VS2005 C++ WinForms .NET 2.0)
There is one form. Second form called from it by button. Call the ShowDialog method is framed by try-catch (Exception^ ex)
When you press button in the Form2 the new Exception is thrown but is not falls in catch, and leads to post UnhandledException Message.
This situation present in Debug and Release builds, but
during step-by-step debugging in VS catch wonderful works.

What should I do to make these exceptions are caught in runtime?

compil commandline DEBUG:
/Od /D "WIN32" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /FD /EHa /MDd /Yu"stdafx.h" /Fp"Debug\nie.pch" /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /nologo /c /Zi /clr:pure /TP /errorReport:prompt /FU ...


compil commandline RELEASE:
/O2 /GL /D "WIN32" /D "NDEBUG" /D "_UNICODE" /D "UNICODE" /FD /EHa /MD /Yu"stdafx.h" /Fp"Release\nie.pch" /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Zi /clr:pure /TP /errorReport:prompt /FU ...



my test App:

C++
//Form1.h
//-----------------------------------------------------
#pragma once
#include "Form2.h"

namespace nie {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			dialogForm2 = gcnew Form2;
		}

	protected:
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  buttonCallForm2;
	protected: 

	private:
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		void InitializeComponent(void)
		{
			this->buttonCallForm2 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
...

		}
#pragma endregion

	private: Form2^ dialogForm2;
	private: System::Void buttonCallForm2_Click(System::Object^  sender, System::EventArgs^  e) {

				 

				 try
				 {
					dialogForm2->ShowDialog();
					MessageBox::Show(L"exit normal");
				 }
				 catch(Exception^ ex)
				 {
					 MessageBox::Show(ex->Message); // not working properly
				 }
				 catch(...)
				 {
					MessageBox::Show(L"oops"); // not working too
				 }

			 }
	};
}

// Form2.h
// ------------------------------
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace nie {

	public ref class Form2 : public System::Windows::Forms::Form
	{
	public:
		Form2(void)
		{
			InitializeComponent();
		}

	protected:
		~Form2()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  buttonEx;
	protected: 

	protected: 

	private:
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		void InitializeComponent(void)
		{
			this->buttonEx = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
...

		}
#pragma endregion
	private: System::Void buttonEx_Click(System::Object^  sender, System::EventArgs^  e) {


				 throw (gcnew System::Exception(L"!!! TEST EXCEPTION !!!"));

			 }
	};
}


Thanks for help!
Posted

First of all, don't try-catch exceptions too locally. It should be done only in cases when you know exact exception type and need to recover from exception in some semantics-specific way. But basically, you should catch all exception on the main even cycle of the UI thread. This is how:
Set exception mode: http://msdn.microsoft.com/en-us/library/ms157905%28v=vs.110%29.aspx[^].
Use System.Windows.Forms.UnhandledExceptionMode.CatchException:
http://msdn.microsoft.com/en-us/library/system.windows.forms.unhandledexceptionmode%28v=vs.110%29.aspx[^].

And handle those exceptions: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception%28v=vs.110%29.aspx[^].

See also my past answers:
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^],
Handling exceptions in class library (dll)[^],
Catching an Exception[^],
Error Logging and Screen Shot.[^],
throw . .then ... rethrowing[^],
When i run an application an exception is caught how to handle this?[^],
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
Keep a c# windows form application running despite any unhandled exception[^].

You should better understand the nature of structural exception handling mechanism:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

—SA
 
Share this answer
 
Comments
ac_code 20-Jan-15 3:00am    
Great thanks for detail explain.
Sergey Alexandrovich Kryukov 20-Jan-15 3:05am    
You are very welcome.
Good luck, call again.
—SA
The exception occurs in dialogForm2 so it needs to be caught in dialogForm2's try/catch block. A test confirms that the exception does get caught in the main form.
 
Share this answer
 
v2
Comments
nv3 19-Jan-15 13:58pm    
That should be true only if dialogForm2 is run as modeless dialog. But it looks like Form2 is called as a modal dialog and hence the try/catch block should be active during the entire lifetime of dialogForm2, doesn't it?
ac_code 19-Jan-15 15:24pm    
If I call Form2 as modeless dialog after it shown form1 switch to next operator (MessageBox) and it is right!
But I need a modal dialog.
While Form2 opened, program stay in try block of Form1, but if Exception formed it goes down, missing catch block.
However, program running under debuger CATCH IT!
Why? It's mean that it can work under special condition of running/compiling, isn't it?
Richard MacCutchan 20-Jan-15 3:32am    
You are correct; I have just run a test and it does indeed catch the exception in the main form.
nv3 20-Jan-15 8:30am    
Thanks for the correction, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900