[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>
Re: [RESOLVED] Error Trapping
Note that it is recommended that new exception classes be derived from ApplicationException rather than Exception.
Re: [RESOLVED] Error Trapping
Why and can you show me an example. :)
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.
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?
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.
Re: [RESOLVED] Error Trapping
So I take it that its not usual to inherit from System.ApplicationException to do you error handling?
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.
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.
Re: [RESOLVED] Error Trapping
An example if you would, please. Havent had any coffee yet. :(