Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to throw exception from one method to another by that if any error occures then the main method will print it in message box. I have already done in c# but trying to write same in vb.net but always giving error when i throw it to another method.Thanks in advance for help.

shwoing this error "
Conversion from string "a" to type 'Integer' is not valid.'
"

What I have tried:

Public Async Function test() As Task(Of DataSet)

Try

Dim ds As New DataSet



Dim a, b As Integer
a = "a"
b = 7
Dim c = a / b


Await Task.Run(Function() adapter.Fill(ds))


Catch ex As Exception
Throw
End Try


Return ds

End Function
Posted
Updated 30-Sep-20 23:31pm
v2
Comments
Richard MacCutchan 1-Oct-20 5:08am    
" always giving error"
Then please tell us what the error is!
Prashant Sharma 1-Oct-20 5:17am    
Conversion from string "a" to type 'Integer' is not valid.'
Richard MacCutchan 1-Oct-20 5:18am    
What part of that do you not understand?
Prashant Sharma 1-Oct-20 5:24am    
sir i understand this error but i want to throw this error to the another method by which i can print it on a message.
Richard MacCutchan 1-Oct-20 5:27am    
You cannot, because that is a compiler error, not a runtime exception. You need to generate a proper exception: see Handling and throwing exceptions in .NET | Microsoft Docs[^].

1 solution

Unless you are also catching exceptions in the code that calls that method, it is doing exactly what it you told it to: throwing an exception, which the debugger or executing app is finding unhandled so it breaks your code.

To be honest, your whole Try block is redundant: since all you do it catch every exception type and rethrow it, you could remove the Try, Catch, Throw, and End Try with no change at all in the outcomes!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900