ProblemAn error has occurred, and you want to inform the user in a friendly manner. SolutionThe captured exception object includes all the details concerning the error, with some parts ready for user-friendly presentation. The simplest presentation option uses the exception's ToString() method to generate information about the error. The following code generates the error message in Figure 15-1 when run within Visual Studio: Try Throw New System.Exception( ) Catch ex As System.Exception MsgBox(ex.ToString( )) End Try Figure 15-1. A basic error messageDiscussionIf you encounter an exception in a block of code where you know errors are likely, you can sometimes compensate for the error through alternate logic without ever informing the user of the problem. In those cases where you cannot continue normally because of the error, your program can inform the user of the situation. Beyond the basic ToString() output, you can handcraft the details of the exception into a form that better communicates the problem to the user. The System.Exception object includes the following useful properties:
Other exception objects further derived from System.Exception may include additional properties with more detailed information. By concatenating the various properties of the captured exception object, you should be able to effectively communicate the problem to the user or store the details in an error log for later analysis. |