Error handling
If the page goes wrong without handling the error, the page displays an error that a user may not be able to read.
In ASP scripts, you can use the
On Error Resume Next
......
If Err.number<>0 Then
Response.Write Err.Description
End If
But what if there is an error in the component? This method can catch errors, but how do you know the exact error?
We can add error handling to the component to return errors so that we can easily see more detailed error messages and help us to troubleshoot the error.
Using Err.Raise, Raise is used to generate Run-time errors
Open VB6 and create a new ActiveX DLL project. Project name modified to fcom, class name modified to FC6
Option Explicit
Public Sub Showerror1 ()
On Error GoTo Errorhandle
Dim I as Double
i = 1/0
Errorhandle:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
' Generate custom errors
Public Sub Showerror2 ()
Err.Raise 600, "your own definition of error 600", "This is the error of describing your own program."
End Sub
OK, a component is written, click the menu-> file-> generate FCom.dll file
OK, there will be a FCom.dll file in the directory
Test
Turn on visual interdev6.0 to generate an ASP file
<%@ Language=vbscript%>
<HTML>
<BODY>
<%
' The following sentence is very important
On Error Resume Next
Set Obj=server. CreateObject ("Fcom.fc6")
Obj.showerror1 ()
' If there is no error handling, it will produce an error interface, very unprofessional
' From 0–512 to a system error, from a 513–65535 range can be used as a user-defined error.
' If the error is reserved, the error number in the component is consistent with the error number of the page processing
If Err.Number <>0 Then
Response.Write "error Message" & Err.Number & Err. Description
End If
Response.Write "<br>"
' If it is a user-defined error, it can be handled separately in the page
Obj.showerror2 ()
If Err.number<>0 Then
If Err.Number =600 Then
Response.Write Err.Number & Err. Source & Err. Description
End If
End If
%>
</BODY>
</HTML>
Configure the virtual directory, in IE to execute this ASP file, the results are as follows:
Error message 11 divisor is zero
600 your own definition of error 600 This is the error of describing your own program