View Full Version : on error resume next statement
charon
04-28-2003, 06:57 AM
hi,
I'm here would like to know about the ASP error handling ->
on error resume next statment.
1.) In order to catch an error which might occur anywhere in an asp page, i out the on error resume next statement on top of the asp page and put the message: if err.number <> 0 then response.write "error has occured" at the bottom of the asp page. Does it correct??
2.) just assume that an error has occurred, the ASP engine will skip that error and continue executed the next line of code right???? I found it when the I failed to register, a successful message still displayed.
Pls help!
come back again,
I'm really confused with the ASP error handling statement on error resume next.
1.) I don't know how many on error statement should I put in one page for detecting any error occur during runtime. Normally, I use on top of every page of my page, and raise the error description at bottom of the page.
2.) I'm really confused on the On Error Resume Next used in Function and Procedure. Different error will be returning if you try to off on on the on error resume next statement in the functions
For instance : I have two three functions :
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
on error resume next
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver
(*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
If err.number <> ) Then
Response.Write err.Description
End If
Set DbConnectionOpen = objDC
End Function
Function RsOpen(ByVal strSQL, ByVal objDC)
Dim objRS
on error resume next
'talk to the database using ADODB.Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
with objRS
.ActiveConnection = objDC
.CursorLocation = adUseClient
.CursorType=adOpenDynamic
.LockType=adLockOptimistic
end with
objRS.Open strSQL, objDC
Set RsOpen = objRS
If err.number <> ) Then
Response.Write err.Description
End If
End Function
Sub callRecord(datacon, tablename)
' on error resume next (on and off for testing)
Set objDC = DbConnectionOpen(DataPath)
sSQL="SELECT * FROM " & tablename
Set objRS = RsOpen(sSQL, objDC)
objRS.AddNew
........
objRS.Update
objRS.Close
Set objRS = Nothing
objDC.Close
Set objDC = Nothing
If err.number <> ) Then
Response.Write err.Description
End If
End Sub
CALL FROM ASP PAGE:
On error resume next is put on top of my page
.
.
.
.
DataPath = Server.MapPath("/data/myData.mdb")
callRecord DataPath, tablename
.
.
.
.
If err.number <> 0 Then
err.Description
End If
When I tried to put the on error resume next statement in the Connection function, this error is displayed:
Error from function: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x870 Thread 0x8cc DBC 0x13c9e9c Jet'.
Error from Asp Page:
The connection cannot be used to perform this operation. It is either closed or invalid in this context.
If I tried to off the on error resume next statement on the connection function, this error will be displayed ASP Page which called the function:
[Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x870 Thread 0x8cc DBC 0x13c9e9c Jet'.
When I tried to On the on error resume next in the callRecord function, things happen differently as well with different error description generated.
3.) May I know how do I put the on error resume next statement correctly in functions and procedures?
Please help????
kalijunfan
04-30-2003, 06:40 AM
Hi Charon,
The On Error Resume Next statement allows you to keep executing your script when an error happens but i donīt think is a good mean to trap errors, even more when it comes to functions as in your case.
Instead of using it you can do something like this:
1. If you are using functions, use error handling inside each one of the functions, so you can customize the message that it displays for the function that failed and not just use Err.Description.
2. As i said, in every function or sub that you use, insert a line like this:
On Error GoTo ErrorControl
Just after declaring all the variables that you are using in that function. Remember using Option Explicit in top of your asp pages forces you to declare all your variables, thatīs a good practice if you are trapping errors.
3. Just before you close your function or sub add the next lines of code:
Exit Function 'Use Exit Sub in case itīs a Sub
ErrorControl:
Response.write("The function NameOfTheFunction produced the following error: " & Err.Number & " - " & Err.Description)
Resume Next
End Function
Here i put your DbConnectionOpen function as an example:
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
On Error GoTo ErrorControl
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*. mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
Set DbConnectionOpen = objDC
Exit Function
ErrorControl:
Response.write("The function DbConnectionOpen produced the following error: " & Err.Number & " - " & Err.Description)
Resume Next
End Function
I hope this helps. By the way, i strongly recommend you to use OLEDB instead of ODBC to connect to a Microsoft Access database, itīs much faster.
Regards
charon
04-30-2003, 06:59 AM
Have question to ask:
Here i put your DbConnectionOpen function as an example:
code:--------------------------------------------------------------------------------
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
On Error GoTo ErrorControl //what will happen if we use on error resume next
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*. mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;ui
d=;pwd=1234"
Set DbConnectionOpen = objDC
Exit Function //Why Exit the function here???
ErrorControl:
//Why Not Exit the function here???
Response.write("The function DbConnectionOpen produced the following error: " & Err.Number & " - " & Err.Description)
Resume Next //Why Put Resume Next here???
End Function
But I was told not used on error goto statement from others forum:
Note:
On error resume next is a basic error trapping statement in asp if I remember right. I would never use a goto statement! That can be very detremental to any code that is written.
Mac
A.) If use goto statement, it will stop from continue execution right and go to the goto ErrorControl, correct??
B.) Is the On Error GoTo ErrorControl same as GoTo 0????
Pls advice!
//Come Back Again
I tried to use :
on error goto ErrorControl
-
-
--
-
ErrorControl:
Response.Write "Error Occured, Sorry for any inconvenience." & err.Description
Error Type:
Microsoft VBScript compilation (0x800A03EA)
Syntax error
/submit.asp, line 6, column 14
on error goto ErrorControl
kalijunfan
05-03-2003, 12:34 AM
Ok, on with the answers:
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
On Error GoTo ErrorControl //what will happen if we use on error resume next
It will go to the next line of code right after the error and
continue excecuting the script no matter if the error was trapped or not
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*. mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
Set DbConnectionOpen = objDC
Exit Function //Why Exit the function here???
Because if you donīt exit the function it will continue with the ErrorControl anyway
no matter if there was any errors or not
ErrorControl:
//Why Not Exit the function here???
Because if an error happens the function will end before you can control it
and you will not know if there was an error
Response.write("The function DbConnectionOpen produced the following error: " & Err.Number & " - " & Err.Description)
Resume Next //Why Put Resume Next here???
This is completely optional, itīs only to show you that if you want to
resume the same function right after you trapped the error you can do it,
just like with a resume next statement, and continue excecuting your asp code.
You donīt have to use it if you donīt want to.
End Function
Ok, now your questions:
A.) If use goto statement, it will stop from continue execution right and go to the goto ErrorControl, correct??
CORRECT
B.) Is the On Error GoTo ErrorControl same as GoTo 0????
No. GoTo 0 disables completely error trapping, not only you donīt know if there was any errors,
you canīt control or trap them in case there was any.
Ok, now about your new error:
Error Type:
Microsoft VBScript compilation (0x800A03EA)
Syntax error
/submit.asp, line 6, column 14
on error goto ErrorControl
This is a syntax error this means that you have something writen bad in there, check your syntax, it could be some punctuation or something else missing IN THE PREVIOUS LINE and when it gets to that line the error jumps, BUT...
If youīve been told that vbscript was not case sensitive thatīs not completely true. in this case it is.
On Error GoTo ErrorControl This is right
on error goto ErrorControl This is wrong, it will give you an error
Thatīs all hope it helped. Bye
charon
05-05-2003, 05:20 AM
Dear kalijunfan,
Thank you so much, you really help me a lot.
1.)Ya, you are right, I use on error goto ErrorControl instead of On Error GoTo ErrorControl
2.)Ok, back to my questions, if I use on error resume next, and if
err.number <> 0 statement at the last part of the function, doesn't it will catch the error and display the error message as well???
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
On Error Resume Next
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
'objDC.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataPath & ";uid=;pwd="
Set DbConnectionOpen = objDC
'Exit function here if there has no error
If err.number <> 0 Then
Response.write("The function DbConnectionOpen produced the following error: " & Err.Number & " - " & Err.Description)
End If
End Function
3.)ok, now our function itself has their own error handler, just assume that in an ASP page which call the function also has on error resume next:
on error resume next
Set conObj = DbConnectionOpen(server.MapPath("mydata.mdb"))
3a.) if we have on error resume next in the ASP page which call the function, if the connection function has error will the ASP continue execute?? will the error message you specify in the function be displayed
3b.) how do I check is the connection established from here
can I use if conObj.errors.count > 0 then
3c.) If I have on error go to ControlHandler statement in the ASP page which call the connection function how does it works in sequence?? which error message will be displayed????
4.) By using the on error resume next statement, just assume that there are 3 errors occur on the page, does the error.description only provides the last error description???
5.) Doesn't the connection object provides error handler -> connection.errors.count can I use the connection.errors.count to check if the database connection is established??
for instance:
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
'objDC.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataPath & ";uid=;pwd="
If objDC.errors.count > 0 Then
Response.write("The function DbConnectionOpen produced the following error: " & objDC.errors.Description
else
Set DbConnectionOpen = objDC
End if
New Found
a forums from ASP emporium stated that vbscript doesnt support GOTO unless it is GOTO 0 to turn off error handling
6.)I use this On Error GoTo ErrorControl This is right also produce syntax error:
Function DbConnectionOpen(ByVal DataPath)
Dim objDC
On Error GoTo ErrorControl
'Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30
'Use this line to use Access
objDC.Open "DBQ=" & DataPath & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"
'objDC.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataPath & ";uid=;pwd="
Set DbConnectionOpen = objDC
'Exit function here if there has no error
Exit Function
ErrorControl:
Response.write "The function DbConnectionOpen produced the following error: " & Err.Number & " - " & Err.Description
End Function
charon
05-07-2003, 03:28 AM
hi, kalijunfan,
pls help me, i'm waiting for your answer to proceed my task....
kalijunfan
05-07-2003, 06:04 AM
Sorry charon, i have a huge load of work and donīt have much time to answer questions in the forums. Here are the answers to your questions:
2.) Ok, back to my questions, if I use on error resume next, and if
err.number <> 0 statement at the last part of the function, doesn't it will catch the error and display the error message as well???
2.) On Error Resume Next is known as Inline Error Handling, this means that it is usefull as long as you handle the error as soon as it happens. So if you have a function that in some part it can have an error, you should use the if err.number thing, right in the next line of that part of the function. If you have some other code in between it will NOT handle the error.
3.)ok, now our function itself has their own error handler, just assume that in an ASP page which call the function also has on error resume next:
on error resume next
Set conObj = DbConnectionOpen(server.MapPath("mydata.mdb"))
3a.) if we have on error resume next in the ASP page which call the function, if the connection function has error will the ASP continue execute?? will the error message you specify in the function be displayed
3b.) how do I check is the connection established from here
can I use if conObj.errors.count > 0 then
3c.) If I have on error go to ControlHandler statement in the ASP page which call the connection function how does it works in sequence?? which error message will be displayed????
3a.) If you are using the On Error GoTo then the error message will be displayed and the asp will continue execute as long as you have the Resume Next statement at the end of the error handling rutine.
3b.) YES. The Connection object has itīs own error object to work with. In your connection function is the best way to handle errors.
3c.) First will handle the errors inside the function and if it doesnīt find any error handling code, it will continue going UP in the asp code until it reaches the start of the code or until it founds the first error handling routine.
4.) By using the on error resume next statement, just assume that there are 3 errors occur on the page, does the error.description only provides the last error description???
4.) asp as an entity in it selft handles one error at a time starting from the begining of the code. If the page has 3 errors will jump the first, you fix it, then jumps the second, you fix it, and then jumps the last one, then you fix it. Thereīs no way that the 3 errors jump together at the same time since the server executes the code as it reads it, from top to bottom. Besides if you have 3 errors at the same time in an asp page, i would think in another line of work instead of web development :)
5.) Doesn't the connection object provides error handler -> connection.errors.count can I use the connection.errors.count to check if the database connection is established??
5.) Answered in point 3b.
a forums from ASP emporium stated that vbscript doesnt support GOTO unless it is GOTO 0 to turn off error handling
6.)I use this On Error GoTo ErrorControl This is right also produce syntax error
Then i donīt know what version of asp my server has since i use On Error GoTo and it works fine for me, but i also develop my own components to work with asp and do the most error handling there so i keep the asp error handling to the minimun.
I donīt know if i mentioned before in the posts, but if you have ASP 3.0 installed in your server then the best thing you can use to handle errors is the ASPError object, itīs much more powerfull than the Error thingy :)
In case you are interested in the ASPError object i recommend you that you buy some asp book that teaches it since teaching it in the forum is out of the question, too long and very little time.
I have Teach Yourself Active Server Pages 3.0 in 21 days (Sams) and Designing Active Server Pages (OīReilly), they both have chapters about error handling using ASPError object.
I hope this helps you
Bye
charon
05-07-2003, 09:35 AM
I have played with the On Error Resume Next by on and off it in ASP page and function. My conclusion is:
1.) Since by putting the On Error Resume Next in The ASP PAGE which call the function already be able to catch the error, why we still need to put it inside the function as it be able to handler the error inside the function as well.
2.) And I found that the error description return by the error object is different between:
a.) With On error resume next inside the function:Error Has Been Occured In The Function. Object required
b.) On error resume next NOT inside the function, BUT only in ASP Page:Error Has Been Occured. [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x7d0 Thread 0x860 DBC 0x13bf014 Jet'.
Can you explain??
3.) The on error resume next inside the function only applicable on the function itself, it is totally nothing to do with the On Error Resume Next in the ASP page, right, it is totally separated.
4.) Finally, I found that the ASP error handler display the last error description. I did it by purpose produce 2 errors
Roy Sinclair
05-07-2003, 04:26 PM
The way I've handled this is to only use this at points where I expect the possibility of errors:
on error resume next ' turn on error handling
err.clear ' clear any existing error
' one or more statements (usually just one) that may cause an error
if err.number <> 0 then
' code to handle the error goes here
end if
on error goto 0 ' turn off error handling
If an error happens outside of a place where I expect it's possible then I usually DO want the page to terminate and display the error.
Also please note that IE defaults to a stupid option that carefully removes all the useful information from a page with an error leaving you with just enough information to tell that an error occured. Make sure you've gone into Tools > Internet Options > Advanced and turned off the "Show Friendly HTTP Error messages". That's a lie, there's nothing "friendly" about taking that useful information from you or an end-user you may be trying to help and simply hiding it.
charon
05-08-2003, 05:19 AM
So sorry that I request to asnwer my questions base on the questions I listed.:))
charon
05-09-2003, 07:19 AM
I hope i can get answer for this question:
1.) Since by putting the On Error Resume Next in The ASP PAGE which call the function already be able to catch the error, why we still need to put it inside the function.
2.) By puttin on error resume next more than one time as below:
On error resume next
-
-
-
-
On error Resume Next
-
-
-
-
-
-
Is same as just put one on error resume next on top of every page right???
whammy
05-10-2003, 11:40 PM
Not really... you shouldn't ever just put On Error Resume Next at the top of every page.
First of all, if you aren't catching the error, your users will never know there's an error, and you'll never know there's an error.
Which means you might never be capturing any data that way! If there's an error that goes unchecked, then you might lose thousands and thousands of potential records...
Also, it's been known to eat up memory on a server if you do this, which will also kill any other websites running on that server...
Personally, I rarely (if ever) use it. And if I do, it's the way Roy Sinclair described...
charon
05-12-2003, 03:25 AM
1.)When I use the error handler, sure I will capture the error. Ok let me tell you why I'm using error hanling, it is for monitor my web-site, if there has any error occur, a notification email will be sending to me.....that all.......WILL IT BE OK??
2.) Still want to know, Since by putting the On Error Resume Next in The ASP PAGE which call the function already be able to catch the error, why we still need to put it inside the function ???
whammy
05-12-2003, 03:44 AM
I guess that depends... where do you WANT to capture errors?
You should treat your application as a your own personal "robot" or "pet"... make it do exactly what you want it to do...
On Error Resume Next
...should be used very rarely. I'm thinking it might help me out if a SQL Server is down. Go ahead and have it email you when there are errors, that definitely helps!
:D
charon
05-12-2003, 04:56 AM
I guess that depends... where do you WANT to capture errors?
1.)So hard for me to explain, anyway, if you said that just use the error handling when I feel it is neccesary, then guess I will use it.
2.)Go ahead and have it email you when there are errors, that definitely helps!
you encourage me to go ehead???
3.) why you never reply my second question
2.) Still want to know, Since by putting the On Error Resume Next in The ASP PAGE which call the function already be able to catch the error, why we still need to put it inside the function ???
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.