Re: Raising errors in .NET
Throw New Exception("DFKJ")
Re: Raising errors in .NET
Quote:
Originally Posted by mendhak
Throw New Exception("DFKJ")
Naughty froggy. :afrog:
Throw New ApplicationException("DFKJ")
Re: Raising errors in .NET
woka... there is a try/catch example app in the 101 VB.NET examples ;)
Re: Raising errors in .NET
also... unless your design model calls for a need to do it like that.. i would move any object disposing code and DB closing code to the finally clause.. so it is run if you error or not...
sort of like this
VB Code:
Public Sub DoFunThings()
Dim CN As SqlConnection
Dim CMD As SqlCommand
Try
CN = New SqlConnection(DBConnString)
CN.Open()
CMD = New SqlCommand("SELECT MARCLAR FROM MARCLAR WHERE [MARCLAR] = MARCLAR", CN)
'PUT SOME CODE HERE THAT DO SOMETHING FUN
Catch ex As SqlException
'ERROR HANDLER HERE FOR DB ERROR
Catch ex As Exception
'ERROR HANDLER HERE FOR ANY OTHER
Finally
Try
If Not CN Is Nothing Then
If CN.State <> ConnectionState.Closed Then
CN.Close()
End If
CN.Dispose()
CN = Nothing
End If
If Not CMD Is Nothing Then
CMD.Dispose()
CMD = Nothing
End If
Catch ex As Exception
End Try
End Try
End Sub
Re: Raising errors in .NET
Also, as you may have noticed, raising exceptions is more costly than vb6 error handling was. There is a noticeable pause prior to the catch block being caught. This makes raising your own errors a less appealing prospect.
When an exception is raised in .NET, I actually get to think "Gee, I wonder what it will be this time?" as I wait for the actual message to come up. I don't remember that long pause using On Error in vb6.
Re: Raising errors in .NET
I have only noticed that when in debug mode... or possibly on the first error raised in a release mode exe... other than that i see no lag in the error handling
Re: Raising errors in .NET
Quote:
Originally Posted by kleinma
I have only noticed that when in debug mode... or possibly on the first error raised in a release mode exe... other than that i see no lag in the error handling
I was wondering about that as I typed it. I, also, have only noticed that in debug mode, but I can't decide if that was because the lag was absent, reduced, or just because I was more attuned to it when in debug mode.
Re: Raising errors in .NET
Quote:
Originally Posted by wossname
Naughty froggy. :afrog:
Throw New ApplicationException("DFKJ")
It would depend on the severity of the exception Woka needs to throw.
If its a non-fatal error then mendhaks corrected example by wossname will be ok.
If its fatal then you need to use -
VB Code:
Throw New System.Exception("Connection Error!")' Or whatever you want to display.
:afrog:
Re: Raising errors in .NET
from MSDN
Quote:
ApplicationException does not provide information as to the cause of the exception. In most scenarios, instances of this class should not be thrown. In cases where this class is instantiated, a human-readable message describing the error should be passed to the constructor.
Re: Raising errors in .NET
Ah, that makes it even worse for the frog :( Time to return your MCAD :p Jk.
You can add the source and description properties of the exception for a useful error message.
VB Code:
Throw New System.Exception(ex.Source & " - " & ex.Message)
Re: Raising errors in .NET
Quote:
Originally Posted by RobDog888
Ah, that makes it even worse for the frog :( Time to return your MCAD :p Jk.
You can add the source and description properties of the exception for a useful error message.
VB Code:
Throw New System.Exception(ex.Source & " - " & ex.Message)
rob.. wouldn't that mean you would already have to have an exception if you are using the ex.source and ex.message?
Like
VB Code:
Try
'something
Catch ex as system.exception
Throw New System.Exception(ex.Source & " - " & ex.Message)
end try
i would think this would be just as good if your goal is to pass the exception up a level
VB Code:
Try
'something
Catch ex as system.exception
Throw ex
end try
Re: Raising errors in .NET
I didnt realize that you could short hand it that way, but yes I was referring to your first example. ;)
Re: Raising errors in .NET
ApplicationWHAT? :ehh:
It's no good because *I* say so.