Answered by:
Change return type of a function on error?

Question
-
User2059472900 posted
Hi
if i got a function with a try cath block inside
and the function should return an integer.
how would i do it that the function can return me a the error message?
public function test() as Integer try do something return integer catch return errormessage (as string) end try End Function
thx
greetings jamal
Monday, January 17, 2011 7:35 PM
Answers
-
User-952121411 posted
Technically a "Try-Catch-Throw" is pointless in the test() method. It is a misconception that exceptions need to be rethrown. In fact it can hinder the process by messing up the callstack when trying to trace where the actual exception occured.
While technically still not incorrect, the "better" or preferred method is to let exceptions bubble up on their own as they will by default. If you can;t actually habdle the exception in the test() method and do something meaningful with it, then letting the exception bubble up to log at the topmost method will work. Take a look or test it out:
Public Sub MainCallingMethod() Try 'Call the method that we know will throw an exception 'and it will bubble up by default test() Catch(ex As Exception) Dim ExMessage As String = ex.Message.ToString() End Try End Sub Public Function test() As Integer Dim i As Integer = 1 Dim j As Integer = 0 'This line will divide by zero and throw an exception on purpose Dim Result As Integer = i / j End Function
You could even do one thing better in the code above and actually test for a 'DivideByZero' exception type if you wanted rather than just type 'Exception'. However the point here is to show you how to allow exceptions no matter how many layers or levels down to bubble up and be caught at the layer that can actually do something meaningful (i.e. log it, display it, etc.)
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 20, 2011 9:44 PM
All replies
-
User-1181492241 posted
Hi, as your function is returing integer, you cannot return the error message. You can try something like this, (The syntax will be wrong)
public function test() as Integer
try
do something
return integer
catch
throw
end try
End Functionpublic function Main
string errorMessage
try
Int x = test()
catch (Exception ex)
errorMessage = ex.Message
throw
end try
End FunctionMonday, January 17, 2011 8:32 PM -
User2059472900 posted
im not @ work so i cannot test it right now,
but i got 2 questions to your reply:
after "throw" do i have to write something?
and in the Main function, wont there just be an error that test has to be an int if i want to set it to X ?
thx
greetings :)
Tuesday, January 18, 2011 4:19 AM -
User-1181492241 posted
throw in test() function - means rethrowing the exception so that you can catch the same exception in Main function.
And in main function you can get exception details and do some thing like show message to user or something...public function Main
string errorMessage
try
Int x = test()
catch (Exception ex)
errorMessage = ex.Message
Response.Write("Error : Details - " + ex.Message)
end try
End FunctionSorry, I didnt understand the second question.
Tuesday, January 18, 2011 8:04 PM -
User2059472900 posted
ok thanks im going to try it
Wednesday, January 19, 2011 7:21 PM -
User-952121411 posted
Technically a "Try-Catch-Throw" is pointless in the test() method. It is a misconception that exceptions need to be rethrown. In fact it can hinder the process by messing up the callstack when trying to trace where the actual exception occured.
While technically still not incorrect, the "better" or preferred method is to let exceptions bubble up on their own as they will by default. If you can;t actually habdle the exception in the test() method and do something meaningful with it, then letting the exception bubble up to log at the topmost method will work. Take a look or test it out:
Public Sub MainCallingMethod() Try 'Call the method that we know will throw an exception 'and it will bubble up by default test() Catch(ex As Exception) Dim ExMessage As String = ex.Message.ToString() End Try End Sub Public Function test() As Integer Dim i As Integer = 1 Dim j As Integer = 0 'This line will divide by zero and throw an exception on purpose Dim Result As Integer = i / j End Function
You could even do one thing better in the code above and actually test for a 'DivideByZero' exception type if you wanted rather than just type 'Exception'. However the point here is to show you how to allow exceptions no matter how many layers or levels down to bubble up and be caught at the layer that can actually do something meaningful (i.e. log it, display it, etc.)
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 20, 2011 9:44 PM