try... catch question.

J

James Radke

Hello,

I have a module which has the flow as follows

Private sub trysomething()
Try

if something....
... some code

if something
dim x as new clsX()
call x.func(parm1)
dim y as new clsY()
call y.func(parm2)
else
dim x as new clsX()
call x.func(parm2)
dim y as new clsY()
call y.func(parm2)
end if
end if

catch ex as exception
msgbox = ex.message
end try

end sub

if in module class clsX - func: I also have try catch block. This try catch
does catch an error, however, it is never passed up to the trysomething
subroutine? How come? Is it because of the nested IF's? This means thet
y.func is getting called even though an error did occur and was caught in
x...

How can I fix this correctly so that if an error occurs in X, the main
trysomething Catch is triggered?

Thanks!

Jim
 
H

Herfried K. Wagner [MVP]

* "James Radke said:
I have a module which has the flow as follows

Private sub trysomething()
Try

if something....
... some code

if something
dim x as new clsX()
call x.func(parm1)
dim y as new clsY()
call y.func(parm2)
else
dim x as new clsX()
call x.func(parm2)
dim y as new clsY()
call y.func(parm2)
end if
end if

catch ex as exception
msgbox = ex.message
end try

end sub

if in module class clsX - func: I also have try catch block. This try catch
does catch an error, however, it is never passed up to the trysomething
subroutine? How come? Is it because of the nested IF's? This means thet
y.func is getting called even though an error did occur and was caught in
x...

You will have to rethrow the error by calling 'Throw' in order to be
able to catch it.
 
A

Armin Zingler

James Radke said:
Code:
if in module class clsX - func: I also have try catch block.  This
try catch does catch an error, however, it is never passed up to the
trysomething subroutine?  How come?[/QUOTE]

If you catch the error in func, it is already caught. Only if there's no
Try..catch in func, the exception is handled in sub trysomething.
 
J

James Radke

What do you mean by "rethrow"?

Will I need Try/catches within the If-then-elses?

Jim
 
H

Herfried K. Wagner [MVP]

* "James Radke said:
What do you mean by "rethrow"?

Will I need Try/catches within the If-then-elses?

You will have to remove the error handler from the procedure you are
calling inside the 'If...Then' and/or throw the catched exception using
'Throw' _inside_ the procedure.
 
J

James Radke

I was under the impression that if you use:

#1) try...
catch
end try

versus

#2 try
catch ex as exception
end try

that the error would be caught, processing halted, AND the exception passed
up to the calling routine... Only if you use #2 would the exception be
fully handled in the routine.. is that not correct?

Jim

Armin Zingler said:
James Radke said:
Code:
if in module class clsX - func: I also have try catch block.  This
try catch does catch an error, however, it is never passed up to the
trysomething subroutine?  How come?[/QUOTE]

If you catch the error in func, it is already caught. Only if there's no
Try..catch in func, the exception is handled in sub trysomething.



--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
[/QUOTE]
 
A

Armin Zingler

James Radke said:
I was under the impression that if you use:

#1) try...
catch
end try

versus

#2 try
catch ex as exception
end try

that the error would be caught, processing halted, AND the exception
passed up to the calling routine... Only if you use #2 would the
exception be fully handled in the routine.. is that not correct?

Both versions are almost equal. #2 additionally supplies access to the
thrown exception. #1 doesn't. That's the only difference. In both versions
the exception is caught and not passed to the calling routine because it's
already handled.

As Herfried wrote, you can rethrow the exception:

try...
catch
'...
throw
end try
 
J

Jay B. Harlow [MVP - Outlook]

James,
that the error would be caught, processing halted, AND the exception passed
up to the calling routine...

You're thinking of
try
...
finally
end try

Because there is no catch clause in the above the Exception will be passed
to the calling routine, however the code in the finally block will be
executed. Useful to close connections without actually doing anything to the
Exception.

As Herfried & Armin pointed out, if you want the exception passed to the
calling routine you need to use Catch with Throw inside.

Hope this helps
Jay


James Radke said:
I was under the impression that if you use:

#1) try...
catch
end try

versus

#2 try
catch ex as exception
end try

that the error would be caught, processing halted, AND the exception passed
up to the calling routine... Only if you use #2 would the exception be
fully handled in the routine.. is that not correct?

Jim

Armin Zingler said:
James Radke said:
Code:
if in module class clsX - func: I also have try catch block.  This
try catch does catch an error, however, it is never passed up to the
trysomething subroutine?  How come?[/QUOTE]

If you catch the error in func, it is already caught. Only if there's no
Try..catch in func, the exception is handled in sub trysomething.



--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
[/QUOTE]
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top