VB Exception ex.Message - vb.net

In c++ the ex.what() function gives me the exact message I wrote when I throw the exception, but in VB when I throw an exception with a custom message and use ex.Message, I get the original message of the exception in addition to mine.
Is there a simple way to only display my custom message?
example:
Throw New ArgumentOutOfRangeException("Invalid Range")
Catch ex As ArgumentOutOfRangeException 'invalid range
MessageBox.Show(ex.Message)
End Try
Message output is:
Instead of just "Invalid Range"

If you throw an ArgumentOutOfRangeException the first parameter in the constructor is ParamName not Message. As this exception is used to indicate when an argument is out of range and ParamName to show which parameter that was.
Example:
Sub MySub(range As Integer)
Try
Throw New ArgumentOutOfRangeException(Nameof(range), "Invalid Range")
Catch ex As ArgumentOutOfRangeException
MessageBox.Show(ex.Message)
End Try
End Sub
If you just want to specify Message you need to write:
Throw New ArgumentOutOfRangeException(nothing, "Invalid Range")

Related

i want to control the error message when calling function return an error

i want to customize my own error box and message when an error occur in calling a function .
enter image description here
i try to use try catch an exception but not doing anything
Try
L = objGeoFlowDLL.GFCalc_Main(nInputs, nOutputs, sngInputs, sngOutputs)
Catch ex As Exception
MessageBox.Show(ex.Message)
' Me.Close()
Finally
End Try
Try
L = objGeoFlowDLL.GFCalc_Main(nInputs, nOutputs, sngInputs, sngOutputs)
Catch ex As DivideByZeroException
MessageBox.Show("Custom message to be shown when something is divided by zero.")
Catch ex2 As Exception
MessageBox.Show("Custom message to be shown when any other error occurs.")
End Try

Occasional Exception Unhandled thrown although Break option is deactivated

I've set up my exceptions so that an error in the code
Try
Using Client As New WebClient
Client.DownloadFile(sExtract, sDownloadTo)
End Using
Catch ex As Exception
Debug.Print("Failed: " + sExtract)
End Try
isn't throw.
This works fine most of the time, but after like 50-100 of errors, the following exception is shown:
According to the checkbox state "Break when this exception type is thrown", which is not-activated, this exception shouldn't be shown this way, right?
What might cause this behaviour, and how could I change it so that this exception isn't thrown?
Here is an additional image of the QuickWatch:
Try
Dim Client As New WebClient
Client.DownloadFile(sExtract, sDownloadTo)
Catch ex As Exception
Console.writeline(ex.tostring)
End Try

VB.Net Exception has been thrown by the target of an invocation

I am using the following code in VB.Net (Winforms) to simply loop through a DataGridView and hide rows that are not needed.
Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
For Each row In Incident_Persons_List.Rows
If Incident_Persons_List.Rows(CInt(row)).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(CInt(row)).Visible = True
Else
Incident_Persons_List.Rows(CInt(row)).Visible = False
End If
Next
End Sub
When the debugger gets to the first line of the IF statement, I get the following error:
An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an
invocation.
I've been trying everything I can think of to understand why this is. I've looked up the fault, but everyone seems to have completely different issues when this exception is thrown.
Is it something to do with how I am doing a compare?
UPDATE 1
I have removed the For Each and replaced it with For i = 0 to Incident_Persons_list.Rows.Count
I have removed the Cint instruction
The Try/Catch as revealed that the actual exception being thrown is:
Row associated with the currency manager's position cannot be made
invisible.
UPDATE 2
Everything is now working correctly with the below code:
Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
Try
For i = 0 To Incident_Persons_List.Rows.Count - 1
If Incident_Persons_List.Rows(i).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(i).Visible = True
Else
'Your code that will throw the Exception
Incident_Persons_List.CurrentCell = Nothing
Incident_Persons_List.Rows(i).Visible = False
End If
Next
Catch ex As TargetInvocationException
'We only catch this one, so you can catch other exception later on
'We get the inner exception because ex is not helpfull
Dim iEX = ex.InnerException
Debug.Print(iEX.Message)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Thanks for the help!
TargetInvocationException :
The exception that is thrown by methods invoked through reflection
How to find out what's going on (because that exception is not really helpfull) ?
You must surroung the calling block with a Try/Catch structure, and then examine the InnerException caught :
Try
'Your code that will throw the Exception
Catch ex As TargetInvocationException
'We only catch this one, so you can catch other exception later on
'We get the inner exception because ex is not helpfull
Dim iEX = ex.InnerException
'Now you can do some stuff to handle your exception
Catch ex As Exception
'Here you catch other kinds of Exceptions that could occur in your code, if you want to...
End Try
And depending on the InnerException, you can now correct your code.
As your row variable is an enumerated element of Incident_Persons_List.Rows and not an index of the element in the collection I think you should replace.
Incident_Persons_List.Rows(CInt(row))
By
row
Or using a basic for structure instead of foreach. Somethink like
For row = 0 To Incident_Persons_List.Rows.Count - 1 Step 1
//SomeStuff
Next

Visual basic "Finally on exception"

I want to run a block of code after the catch statements regardless of which exception happens but only when some exception happens. So basically it's a finally statement that requires an exception in order to execute. Is there an easy way to do this in visual basic?
How about
Dim isException As Boolean = False
Try
....
Catch ex As ApplicationException
isException = True
....
Catch ex As Exception
isException = True
....
Finally
If (isException)
....
End If
End Try
I don't like it, but how about a nested Try:
Try
Try
....
Catch ex As ApplicationException
Throw
Catch ex As Exception
Throw
End Try
Catch
' This is your "finally"
End Try
The finally block of the If statement will always be called regardless if there is an exception raised or not. MSDN
The only other way to do this is to have another method that accepts an exception as a parameter and call that method from the exception catch.
You could add different exception blocks, like this:
Try
' do operation
' Most specific:
Catch e As ApplicationException
' do something only if ApplicationException has occurred
' Least specific:
Catch e As Exception
Console.WriteLine("{0} Second exception caught.", e);
End Try

Error handling message types

I am using try..catch for error handling. I am getting the message displayed as
messagebox.show (ex.tostring)
But it gives very long message.
Is it possible just to get only the actual error or I could give my own modified message, based on what ex contains?
Thanks
Use Exception.Message property.
MessageBox.Show(ex.Message)
You can print the content of the Message property. Usually it's a short descriptive message without the full - technical - details of the stack.
Exception.Message contains a simple description of the exception (e.g. "Object reference not set...").
Exception.ToString() contains a description of the exception along with a complete stack trace.
The Message property returns only the message(which explains the reason for the exception).
Dim message As String = "Message: " & ex.Message
MessageBox.Show(message)
However, if you only want the name of the Exception's Type:
Dim typeName = ex.GetType().Name
If you want less text in message then try to change
Exception.ToString to Exception.Message
as Massimiliano Peluso said, and if you want to customize then I hope this will give you some idea.
Try
'Your Codes...
Catch oledbEx As OleDbException
MessageBox.Show("Your message")
Catch ex As Exception
MessageBox.Show("Your message")
Catch ioEX As IOException
MessageBox.Show("Your message")
Catch dataEX As DataException
MessageBox.Show("Your message")
End Try