err.raise + scoping

  • Thread starter Thread starter abcd
  • Start date Start date
A

abcd

this is simple VB question but its not clicking me right now...the code is
as below


Private Sub Command1_Click()

On Error Resume Next

MsgBox calculate

End Sub

Function calculate() As String

On Error GoTo errH

x = 10
y = 0

k = x / y

calculate = "successful"

Exit Function

errH:

calculate = "failed"

Err.Raise 1234, "dsfsd", "test message"

End Function


'I am expecting that calculate should return "failed" but its returning
empty,

What could be the cause...basically I want to get a value as a return value
from the error handler...

[its shame on me that I am microsoft certified in VB 6.0 (both desktop and
distributed and I can not solve this]
 
Ohhhh. Well you should be trapping an exception here:




Private Sub Command1_Click()
MsgBox calculate
End Sub

Function calculate() as String

Dim x as integer = 10
Dim y as integer = 0
Dim k as integer

Try

k = ( x / y )

Return "Success"

Catch Ex as Exception

Return "Failure"

End Try

End Function
 
Thanks. You are right.

I found alternate solution like passing a returnvariable byref and getting
the result value into that variable....(like C++ out parameter)

thanks



Jonathan Allen said:
You raised an error, so the function cannot return a value.

--
Jonathan Allen


abcd said:
this is simple VB question but its not clicking me right now...the code
is as below


Private Sub Command1_Click()

On Error Resume Next

MsgBox calculate

End Sub

Function calculate() As String

On Error GoTo errH

x = 10
y = 0

k = x / y

calculate = "successful"

Exit Function

errH:

calculate = "failed"

Err.Raise 1234, "dsfsd", "test message"

End Function


'I am expecting that calculate should return "failed" but its returning
empty,

What could be the cause...basically I want to get a value as a return
value from the error handler...

[its shame on me that I am microsoft certified in VB 6.0 (both desktop
and distributed and I can not solve this]
 

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

Back
Top