Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

14.17 Exception Handling

Handling Exceptions

Crystal Reports gives you the ability to handle any errors that might occur while printing and previewing. The benefit to handling the error yourself is that you can customize the error handling process. For example, you could write the error to a log file or you can gracefully exit the process without throwing an error message at the user.

The CrystalReportViewer class has an Error() event that you can subscribe to. It is fired whenever an error occurs. This event has properties to tell you the exception information and give you the option to handle it yourself. Table 14-12 lists the properties for the ExceptionEventArgs() type.

Table 14-12. Properties of the ExceptionEventArgs event type.

Property Description
Exception The exception class for getting the error number and description.
Handled Set to True if you do not want the standard error message shown.

Here is sample code for handling a report error.

Listing 14-11. Handling report exceptions.
Private Sub CrystalReportViewer1_Error(ByVal source As System.Object, _
ByVal e As CrystalDecisions.Windows.Forms.ExceptionEventArgs) _
Handles CrystalReportViewer1.Error
‘put your code to handle the error here - maybe save to a log file?
‘…
‘Tell the viewer that you handled the error and suppress the error message
e.Handled = True
End Sub

If you aren’t using the viewer control, e.g. exporting reports, the ReportDocument class doesn’t have any error handling events associated with it. You will have to use the standard Try Catch method of trapping errors.