How to catch an exception and show its details [closed]

时光怂恿深爱的人放手 提交于 2019-12-11 09:55:52

问题


I'm wondering how I'd go about catching an error and then displaying its details in a custom ListBox or 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.

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.


回答1:


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.

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



回答2:


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!




回答3:


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



回答4:


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 Label and RichTextBox on it. You could give it a constructor that takes the exception and will populate the form's controls. I'd use ToString on the exception to show details since this will print out a nice stacktrace and also print out any details of inner exceptions recursively:

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



回答5:


You could write a function to use in place of MsgBox e.g.:

   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.



来源:https://stackoverflow.com/questions/20955027/how-to-catch-an-exception-and-show-its-details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!