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 a HandleException() event that you can subscribe to. It is fired whenever an error occurs. This event has properties to tell you the exception class that was raised and lets you specify a new text message for the error. Table 14-10 lists the properties for the ExceptionEventArgs() type.
Table 14-10. Properties of the ExceptionEventArgs event type.
Property | Description |
---|---|
Exception | The class of exception that was raised. |
Handled | Set to True if you do not want to the error triggered. |
UserData | Overrides the error message. |
Unfortunately, the HandleException() event is not available if you are using the ReportDocument class. If you are printing a report directly with the ReportDocument class, there is no error-related event that you subscribe to. You have to use the standard Try-Catch error handling statements that you use for the rest of your application.
Try
Dim myReport As New CrystalReport1()
myReport.PrintToPrinter(1, False, 0, 0)
Catch
... do error handling here
End Try