|
-
Apr 19th, 2005, 05:51 AM
#1
Raising errors in .NET
VB6:
VB Code:
Dim objConn As Connection
On Error Goto ErrHandler:
'code
Exit Sub
ErrHandler:
If Not (objConn Is Nothing) Then
objConn.Close
Set objConn = Nothing
End If
Err.Raise Err.Number, Err.Source, Err.Description
VB.NET
VB Code:
Dim Conn As SQLConnection
Try
'code
Catch Ex As Exception
If Not (Conn Is Nothing) Then
Conn.Close
Conn = Nothing
End If
'Want to raise exception to UI here
End Try
How do I do this?
Woka
-
Apr 19th, 2005, 06:10 AM
#2
Re: Raising errors in .NET
Throw New Exception("DFKJ")
-
Apr 19th, 2005, 09:15 AM
#3
Re: Raising errors in .NET
 Originally Posted by mendhak
Throw New Exception("DFKJ")
Naughty froggy.
Throw New ApplicationException("DFKJ")
I don't live here any more.
-
Apr 19th, 2005, 09:17 AM
#4
Re: Raising errors in .NET
woka... there is a try/catch example app in the 101 VB.NET examples
-
Apr 19th, 2005, 09:22 AM
#5
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
-
Apr 19th, 2005, 10:53 AM
#6
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.
My usual boring signature: Nothing
 
-
Apr 19th, 2005, 10:55 AM
#7
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
-
Apr 19th, 2005, 11:31 AM
#8
Re: Raising errors in .NET
 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.
My usual boring signature: Nothing
 
-
Apr 19th, 2005, 11:54 AM
#9
Re: Raising errors in .NET
 Originally Posted by wossname
Naughty froggy.
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 19th, 2005, 12:04 PM
#10
Re: Raising errors in .NET
from MSDN
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.
-
Apr 19th, 2005, 12:40 PM
#11
Re: Raising errors in .NET
Ah, that makes it even worse for the frog Time to return your MCAD 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)
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 19th, 2005, 01:25 PM
#12
Re: Raising errors in .NET
 Originally Posted by RobDog888
Ah, that makes it even worse for the frog Time to return your MCAD 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
-
Apr 19th, 2005, 01:29 PM
#13
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 19th, 2005, 04:34 PM
#14
Addicted Member
Even shorter
 Originally Posted by RobDog888
I didnt realize that you could short hand it that way
VB Code:
Try
'something
Catch ex As System.Exception
Throw
End Try
-
Apr 20th, 2005, 12:06 AM
#15
Re: Raising errors in .NET
ApplicationWHAT? 
It's no good because *I* say so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|