ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   question error handling (https://www.excelbanter.com/excel-programming/344942-question-error-handling.html)

Pierre via OfficeKB.com[_2_]

question error handling
 
hi experts,

i have a simple error handling question.

i have the following code:

private sub test()
on error goto errorhandling

my stuff

exit sub
errorhandling:
My error
end sub

Here's my question:
if in 'my stuff' i have, next to some calculation code, a module i want tot
execute, do i have to put the 'on errror' codelines also in the module is is
it enough to have it in the main code above?

Thanks,
Pierre


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200511/1

Don Guillett[_4_]

question error handling
 
Have you tried it before asking?

--
Don Guillett
SalesAid Software

"Pierre via OfficeKB.com" <u13950@uwe wrote in message
news:5704aed09da13@uwe...
hi experts,

i have a simple error handling question.

i have the following code:

private sub test()
on error goto errorhandling

my stuff

exit sub
errorhandling:
My error
end sub

Here's my question:
if in 'my stuff' i have, next to some calculation code, a module i want

tot
execute, do i have to put the 'on errror' codelines also in the module is

is
it enough to have it in the main code above?

Thanks,
Pierre


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200511/1



Jake Marx[_3_]

question error handling
 
Hi Pierre,

Pierre via OfficeKB.com wrote:
private sub test()
on error goto errorhandling

my stuff

exit sub
errorhandling:
My error
end sub

Here's my question:
if in 'my stuff' i have, next to some calculation code, a module i
want tot execute, do i have to put the 'on errror' codelines also in
the module is is it enough to have it in the main code above?


It depends on what you want the code to do. By default, if you call a
function or subroutine, and it has no error handling of its own, the error
will be "passed up" to the calling code. So the error handler for this sub
"test" will catch the error. If the other function or subroutine does have
error handling, it will catch the error, and you can decide what to do with
it there.

BTW, it's best to use a Resume statement after you trap an error, as the
next error your code encounters will be untrappable if you don't. Here's
what I typically do:

Sub Test()
On Error GoTo ErrHandler

'/ code here

ExitRoutine:
'/ clean up here (set object vars to Nothing, etc)
Exit Sub
ErrHandler:
'/ handle error here if needed
Resume ExitRoutine
End Sub

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]



Pierre via OfficeKB.com[_2_]

question error handling
 
Thanks Jake,
This was a big help !
Pierre

Jake Marx wrote:
Hi Pierre,

private sub test()
on error goto errorhandling

[quoted text clipped - 10 lines]
want tot execute, do i have to put the 'on errror' codelines also in
the module is is it enough to have it in the main code above?


It depends on what you want the code to do. By default, if you call a
function or subroutine, and it has no error handling of its own, the error
will be "passed up" to the calling code. So the error handler for this sub
"test" will catch the error. If the other function or subroutine does have
error handling, it will catch the error, and you can decide what to do with
it there.

BTW, it's best to use a Resume statement after you trap an error, as the
next error your code encounters will be untrappable if you don't. Here's
what I typically do:

Sub Test()
On Error GoTo ErrHandler

'/ code here

ExitRoutine:
'/ clean up here (set object vars to Nothing, etc)
Exit Sub
ErrHandler:
'/ handle error here if needed
Resume ExitRoutine
End Sub



--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200511/1


All times are GMT +1. The time now is 12:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com