vb.net 如何捕获异常并显示其详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20955027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch an exception and show its details
提问by user2980316
I'm wondering how I'd go about catching an error and then displaying its details in a custom ListBoxor something. The idea is to allow a custom message to appear that's simple, like "Woops, something's went wrong!" but still be able to provide the information for troubleshooting. If someone could help me build this code I'd be really grateful.
我想知道如何捕获错误,然后在自定义ListBox或其他内容中显示其详细信息。这个想法是允许出现一个简单的自定义消息,比如“糟糕,出了点问题!” 但仍然能够提供故障排除的信息。如果有人可以帮助我构建此代码,我将不胜感激。
So say I have code that could result in an error, like connecting to the internet. How would I be able to catch the error and display it in a separate form (pop-up window)?
所以说我有可能导致错误的代码,比如连接到互联网。我如何才能捕获错误并以单独的形式(弹出窗口)显示它?
I apologize if this seams really basic but I'm new to this stuff and I just don't like the normal error window.
如果这个接缝真的很基本,我很抱歉,但我对这些东西很陌生,我只是不喜欢正常的错误窗口。
回答by Max
Use following code to output error to user:
使用以下代码向用户输出错误:
Try
'code
Catch ex As Exception
MessageBox.Show(string.Format("Error: {0}", ex.Message))
End Try
I'm wondering how I'd go about catching an error and then displaying its details in a custom ListBox or something.
我想知道如何捕获错误,然后在自定义 ListBox 或其他内容中显示其详细信息。
If you would like to add the error to a listbox:
如果您想将错误添加到列表框中:
Try
'code
Catch ex As Exception
listBox1.Items.Add("Whoops, something went wrong!")
End Try
回答by user2980316
In the end I ended up with this:
最后我得到了这个:
Try
'Code which may error
Catch ex As Exception
MessageBox.Show("Whoops! An error was encountered during the login-in stage. The Server may be offline, un-reachable or your Server Credentials may be in-correct. Please contact U.G Studio for further details. " & _
vbNewLine & "" & vbNewLine & String.Format("Error: {0}", ex.Message))
It allows me to display a custom message whilst still retaining the 'technical' information about the error.
它允许我显示自定义消息,同时仍保留有关错误的“技术”信息。


Thanks for the help people!
感谢帮助的人!
回答by Neolisk
Here is a code template to get you started:
这是一个可以帮助您入门的代码模板:
Try
'Your code
Catch ex As Exception
MessageBox.Show("Woops, something's went wrong!")
'get troubleshooting info out of ex, stack trace perhaps
End Try
回答by Jeff B
If you want something super simple, you could do this:
如果你想要一些超级简单的东西,你可以这样做:
Try
'Try connecting to the internet
Catch ex As WebException
Dim message = String.Format(
"Encountered an error while connecting to internet: {0}", ex.Message)
MessageBox.Show(message)
End Try
But if you want something a bit more fancy, I'd recommend creating a new form with maybe a Labeland RichTextBoxon it. You could give it a constructor that takes the exception and will populate the form's controls. I'd use ToStringon the exception to show details since this will print out a nice stacktrace and also print out any details of inner exceptions recursively:
但是,如果您想要更花哨的东西,我建议您创建一个可能带有Label和的新表单RichTextBox。你可以给它一个构造函数,它接受异常并填充表单的控件。我会ToString在异常上使用来显示详细信息,因为这将打印出一个很好的堆栈跟踪,并递归地打印出内部异常的任何详细信息:
Public Sub New(ex As Exception)
InitializeComponent() 'This call is required by Visual Studio.
Me.Label1.Text = String.Format(
"Encountered the following error: {0}", ex.Message)
Me.RichTextBox1.Text = ex.ToString()
End Sub
You could call it from your main form like this:
您可以像这样从主表单中调用它:
Try
'Try connecting to the internet
Catch ex As WebException
Dim errorForm = New ErrorForm(ex)
errorForm.Show()
End Try
回答by rheitzman
You could write a function to use in place of MsgBox e.g.:
您可以编写一个函数来代替 MsgBox 例如:
Public Function LogMsgBox(
ex As Exception,
Prompt As String,
Optional Buttons As MessageBoxButtons = MessageBoxButtons.OK,
Optional Title As String = "OIS Error",
Optional ProgrammerNote As String = "",
Optional SuppressMsgBox As Boolean = False,
Optional FormRef As Object = Nothing) As MsgBoxResult
In the function you can get as fancy as you like - most likely show a dialog box or form more to your liking.
在该功能中,您可以随心所欲 - 最有可能根据您的喜好显示对话框或表单。
It is handy to log errors so you get feedback on problems. Users just tend to click by problems and not report them.
记录错误很方便,因此您可以获得有关问题的反馈。用户只是倾向于点击问题而不是报告问题。
Also look into Application Framework for VB - it can capture unhandled errors the users also fail to report.
还可以查看 VB 应用程序框架 - 它可以捕获用户也未报告的未处理错误。

