Most exception handlers are designed to trap exceptions that aren't anticipated at design time. Use a Try Catch Finally structure to catch all unanticipated exceptions.
Private Sub btnSelectPicture_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles _ btnSelectPicture.Click ' Purpose : Open the selected picture and display it in the ' picture box. ' Show the open file dialog box. If ofdSelectPicture.ShowDialog = DialogResult.OK Then ' Load the picture into the picture box. picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName) ' Show the name of the file in the form's caption. Me.Text = "Picture Viewer(" & ofdSelectPicture.FileName & ")" End If End Sub
Private Sub btnSelectPicture_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles _ btnSelectPicture.Click ' Purpose : Open the selected picture and display it in the ' picture box. Try ' Show the open file dialog box. If ofdSelectPicture.ShowDialog = DialogResult.OK Then ' Load the picture into the picture box. picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName) ' Show the name of the file in the form's caption. Me.Text = "Picture Viewer(" & ofdSelectPicture.FileName & ")" End If Catch ex As Exception MessageBox.Show("Error: " & e.Message, "Error!", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try End Sub
When you use Try Catch Finally, it's important to use a consistent format for handling unanticipated exceptions. It's best to use a central exception handler, but if you don't use one, create an exception handler like this typical Catch block:
Catch ex As Exception MessageBox.Show("Exception: " & ex.Message & ControlChars.CrLf & _ ControlChars.CrLf & _ "Module: " & modulename & ControlChars.CrLf & _ "Method: " & ex.TargetSite.Name)
The only part of the MessageBox.Show statement that should be modified for each procedure is the module.
You can add code to the Catch block at your discretion, such as invoking a rollback on a database transaction. Of course, the necessary code will vary from procedure to procedure, and some code might best be placed in the Finally block, depending on the situation at hand.
Creating a central exception handler eliminates the necessity to micromanage the exception handler in every procedure. However, if you don't use a central exception handler, you must handle and display exceptions in a consistent manner.
Catch ex As Exception MessageBox.Show("An unexpected exception has occurred." & _ ControlChars.CrLf & ex.Message) Catch ex As Exception MessageBox.Show("An error has occurred!", "Error!", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex As Exception MessageBox.Show("Exception: " & ex.Message & _ ControlChars.CrLf & ControlChars.CrLf & _ "Module: " & Me.Name & ControlChars.CrLf & _ "Method: " & ex.TargetSite.Name) Catch ex As Exception MessageBox.Show("Exception: " & ex.Message & _ ControlChars.CrLf & ControlChars.CrLf & _ "Module: " & mc_ModuleName & ControlChars.CrLf & _ "Method: " & ex.TargetSite.Name)
When you display an error message to a user, never blame the user or make the user feel he or she did something wrong. If the exception that has been caught was not anticipated, the fault lies with the programmer. If the exception was a result of "bad" user input, find a way to politely inform the user of the situation rather than make the user feel as bad as the data. Make sure the user can tell the difference between a programming error and a legitimate exception (an anticipated error).
Dim intTotal As Integer Dim intPeriods As Integer Try ' Get the total and number of periods from the user. intTotal = CInt(txtTotal.Text) intPeriods = CInt(txtPeriods.Text) ' Display the payment. MessageBox.Show("Each payment is: " & _ CSng(intTotal / intPeriods), _ "Payment Info", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Catch ex As InvalidCastException ' The user didn't enter a numeric value! MessageBox.Show("The total can't be computed because you " & _ "failed to enter a numeric value.", _ "User Error", MessageBoxButtons.OK, _ MessageBoxIcon.Warning) Catch ex As Exception MessageBox.Show("Exception: " & ex.Message & ControlChars.CrLf & _ ControlChars.CrLf & _ "Module: " & "test" & ControlChars.CrLf & _ "Method: " & ex.TargetSite.Name) End Try
Dim intTotal As Integer Dim intPeriods As Integer Try ' Get the total and number of periods from the user. intTotal = CInt(txtTotal.Text) intPeriods = CInt(txtPeriods.Text) ' Display the payment. MessageBox.Show("Each payment is: " & _ CSng(intTotal / intPeriods), _ "Payment Info", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Catch ex As InvalidCastException ' The user didn't enter a numeric value. MessageBox.Show("Please enter a numeric value in each " & _ "required field.", "MyApplication", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show("Exception: " & ex.Message & ControlChars.CrLf & _ ControlChars.CrLf & _ "Module: " & "test" & ControlChars.CrLf & _ "Method: " & ex.TargetSite.Name) End Try
Tip
Whenever possible, offer the user practical advice for dealing with an exception.