|
-
Jul 14th, 2005, 02:27 PM
#1
[RESOLVED] Error Trapping
It used to be asey in VB6 to trap for a particular error number but not now. 
I want to trap for the failure of a SQL connection. Since there can be many reasons for failure I want to trap by the error number.
I found it during runtime in what looked like ex.innermessage.number but I cant get to it. How do I do it properly?
TIA.
VB Code:
'<CONNECT TO SQL DB>
Try
goSQLConn = New SqlConnection
goSQLConn.ConnectionString = "Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=SSPI;"
goSQLConn.Open()
Catch ex As Exception
If ex.number = 4060 Then '<-- no dice :(
End If
End Try
'</CONNECT TO SQL DB>
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 
-
Jul 14th, 2005, 02:34 PM
#2
Re: Error Trapping
don't have .NET on this pc..but did you try
err.number?
-
Jul 14th, 2005, 02:37 PM
#3
Re: Error Trapping
Yup, and it didnt like it. It said for me to stop writting code using VB6 syntax.
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 
-
Jul 14th, 2005, 02:41 PM
#4
Re: Error Trapping
kfcSmitty - the err object no longer exists in .NET.....
RD - dang it.... I know I just saw a post on this exact thing recently..... search for posts with my username and "exception" and "error" and "handling"...... You might have to dig a little, but there's a thread out there that has sample code on how to do that.
Tg
-
Jul 14th, 2005, 02:43 PM
#5
-
Jul 14th, 2005, 02:45 PM
#6
-
Jul 14th, 2005, 02:45 PM
#7
Re: Error Trapping
try this...
Or look up SqlConnection.Open in MSDN. It has a list of exceptions thrown by this class.
Exceptions come in many shapes and forms, they are probably the most commonly derived classes in .Net.
All exceptions ultimately derive from Class Exception, but lets say you invented a class that does lots of complex things and could go wrong in many ways, you'd first create a class called RobDogException which inherits from Exception.
Then when you want to make this new class the base class of all your custom exceptions that are unique to your class. Some people call this an Exception family. I usually make an exception family for all my large classes, makes your code more robust and makes life easier for coders using your class to track down problems.
-
Jul 14th, 2005, 02:52 PM
#8
Re: Error Trapping
w00t w00t, I got it from Tgs link where Kleinma shows the SQLException object.
VB Code:
'<CONNECT TO SQL DB>
Try
goSQLConn = New SqlConnection
goSQLConn.ConnectionString = "Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=SSPI;"
goSQLConn.Open()
Catch exSQL As SqlException
If exSQL.Errors.Item(0).Number = 4060 Then
MessageBox.Show(exSQL.Errors.Item(0).Message, exSQL.Errors.Item(0).Number.ToString _
& ": " & exSQL.Errors.Item(0).Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'<CONNECT TO SQL DB>
Last edited by RobDog888; Jul 14th, 2005 at 03:12 PM.
Reason: Code is too wide.
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 
-
Jul 14th, 2005, 02:55 PM
#9
Re: Error Trapping
Didnt refresh before I posted. Didnt see your post wossy. It was right on though. 
I digged into the exsql and found the number property. 
Thanks Guys,
Scooby snacks for everyone! 
I must be having a good day
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 
-
Jul 14th, 2005, 02:59 PM
#10
Re: [RESOLVED] Error Trapping
Note that it is recommended that new exception classes be derived from ApplicationException rather than Exception.
-
Jul 14th, 2005, 03:02 PM
#11
Re: [RESOLVED] Error Trapping
Why and can you show me an 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 
-
Jul 14th, 2005, 03:12 PM
#12
Re: [RESOLVED] Error Trapping
I read the help topic "Introduction to Exception Handling in Visual Basic .NET" just the other day, and slightly misread it, it would seem. It says that there two exception base classes: System.Exception and Application.Exception. It says that when creating new exception classes we should inherit them from Application.Exception. Whoops, missed the dot. There's an example in that help topic.
Hey, now I'm reading the help topic "ApplicationException Class" and it says we should inherit from System.ApplicationException. The aforementioned example does just that, so I was right after all. Somebody stuffed up the help and it wasn't me.
-
Jul 14th, 2005, 03:25 PM
#13
Re: [RESOLVED] Error Trapping
I didnt realize that you could inherit from the exception object. What would be the correct way to error handle then since
there are so many as compared to vb6?
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 
-
Jul 14th, 2005, 07:23 PM
#14
Re: [RESOLVED] Error Trapping
In most circumstances I find that I don't care what type of exception has been thrown, only that one has been. Therefore, I usually just use a single Catch block for an Exception that handles all cases. Every Framework method that can throw an exception has details of the types it can throw in the documentation, so you always know what exception types you need to consider, if you do indeed want to handle them differently. Any developer who creates and distributes their own components should include similar documentation. That way the types of exceptions you need to consider for any operation are known beforehand and can be handled accordingly.
-
Jul 14th, 2005, 08:30 PM
#15
Re: [RESOLVED] Error Trapping
So I take it that its not usual to inherit from System.ApplicationException to do you error handling?
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 
-
Jul 14th, 2005, 08:41 PM
#16
Re: [RESOLVED] Error Trapping
I only really do small time stuff, but I've never had the need to create my own custom exception classes. I've always found those provided by the Framework to be sufficient. If you do have a need for a custom exception, according to the documentation you should inherit from ApplicationException rather than Exception. If I remember what I read properly, ApplicationException inherits from Exception but adds no new functionality. Inheriting from it simply indicates that your exception was thrown by the application rather than the system.
-
Jul 15th, 2005, 08:31 AM
#17
Re: [RESOLVED] Error Trapping
All you need to do in order to catch exceptions is to know which ones too look out for. Lets say you have 5 different ones that could all be thrown by a class. Look to see which is the base exception that all 5 are derived from and then just CATCH that.
Easy.
I don't live here any more.
-
Jul 15th, 2005, 09:43 AM
#18
Re: [RESOLVED] Error Trapping
An example if you would, please. Havent had any coffee yet.
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 
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
|