在C ++ CLI应用程序中捕获用于登录的异常 [英] Trapping exceptions for logging in a C++ CLI app

查看:224
本文介绍了在C ++ CLI应用程序中捕获用于登录的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C ++ / CLI应用程序中捕获任何和所有异常,以便我可以记录和记录它们(包括堆栈跟踪)。到目前为止我有一些看起来很有前途的代码:

I'm trying to trap any and all exceptions in a C++/CLI app so that I can log and record them (including a stack trace). So far I have some code which looked promising:

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	try
	{
		Application::Run(gcnew MainForm());
	}
	catch( System::Exception^ e )
	{
		String^ message = gcnew String("");
		System::Exception^ exceptionRecursor = e;

		message = "ERROR: Uncaught exception encountered!\n\n";
		while( exceptionRecursor )
		{
			message += exceptionRecursor->Source+"\n\t";
			message += exceptionRecursor->Message+"\n\t";
			message += exceptionRecursor->StackTrace+"\n\n";
			exceptionRecursor = exceptionRecursor->InnerException;
		}
		MessageBox::Show(message);
	}

	return 0;
}

...但是不是取消一个对话框,我得到的东西:

...but instead of disaplying a dialog box with my tidied up errors, I get something else:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

这是因为 Run 命令正在尝试以某种方式处理异常?我需要处理 MainForm 里面的东西吗? ...或是有其他(更好的)这种方式。

Is this because the Run command is trying to deal with the exception in some way? Do I need to handle things inside MainForm somewhere? ...or is there some other (better) way of going about this.

忘记错误的源头一会儿(我在中期的开发周期,调试),很高兴能够捕获这些错误,并产生一个整洁的小堆栈跟踪,可以保留在代码中直到部署,让用户我们知道什么时候出了问题。

Forgetting the source of the error for a moment (I'm mid development cycle and still debugging), it would be nice to be able to trap these errors and produce a neat little stack trace which could remain in the code right up to deployment and let users us know when things are going wrong. Eventually I'd wrap the error report into something that could report over the web.

推荐答案

我发现了一个解决方案href =http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx =nofollow> Application :: ThreadException ):

I found a solution (using Application::ThreadException):

// Creates a class to throw the error.
public:
   ref class ErrorHandler: public System::Windows::Forms::Form
   {
	  // Inserts the code to create a form with a button.

	  // Programs the button to throw an exception when clicked.
   private:
	  void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
	  {
		 throw gcnew ArgumentException( "The parameter was invalid" );
	  }

   public:
	  static void Main()
	  {
		 // Creates an instance of the methods that will handle the exception.
		 CustomExceptionHandler ^ eh = gcnew CustomExceptionHandler;

		 // Adds the event handler to to the event.
		 Application::ThreadException += gcnew ThreadExceptionEventHandler( eh, &Form1::CustomExceptionHandler::OnThreadException );

		 // Runs the application.
		 Application::Run( gcnew ErrorHandler );
	  }
   };

// Creates a class to handle the exception event.
internal:
   ref class CustomExceptionHandler
   {
	  // Handles the exception event.
   public:
	  void OnThreadException( Object^ /*sender*/, ThreadExceptionEventArgs^ t )
	  {
		 System::Windows::Forms::DialogResult result = ::DialogResult::Cancel;
		 try
		 {
			result = this->ShowThreadExceptionDialog( t->Exception );
		 }
		 catch ( Exception^ ) 
		 {
			try
			{
			   MessageBox::Show( "Fatal Error", "Fatal Error", MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
			}
			finally
			{
			   Application::Exit();
			}
		 }

		 // Exits the program when the user clicks Abort.
		 if ( result == ::DialogResult::Abort )
		 {
			Application::Exit();
		 }
	  }

	  // Creates the error message and displays it.
   private:
	  System::Windows::Forms::DialogResult ShowThreadExceptionDialog( Exception^ e )
	  {
		 String^ errorMsg = "An error occurred please contact the adminstrator with the following information:\n\n";
		 errorMsg = String::Concat( errorMsg, e->Message, "\n\nStack Trace:\n", e->StackTrace );
		 return MessageBox::Show( errorMsg, "Application Error", MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
	  }
   };

这篇关于在C ++ CLI应用程序中捕获用于登录的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆