Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Error Exception Number

Status
Not open for further replies.

lucyv

Programmer
Mar 11, 2002
152
US
In my program I'm trying to catch any errors that occur and want to perform certain functions depending on the type of error. In VB6 I was able to do this using the Err object (err.number), but in .NET the Exception object does not appear to have a property to identify the error. Is there any way I kind identify what type of error has ocurred?

Thanks in advance.

-lucyv
 
You can still use the Err object in VB .NET:

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Try
    Throw New FileNotFoundException("File not found!")
  Catch ex As Exception
    MsgBox(Err.Number)
  End Try
End Sub

This will result in a messagebox displaying '53', which is the error number for file not found. The Err object is automatically populated when an exception occurs.

Even though the documentation states that you can use the Err object only when using the On Error GoTo statement, it works this way too.

To prove that it does work you can also try the following code, which reverses the process:

Code:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Try
    Err.Raise(53)
  Catch ex As ApplicationException
    'Code to intercept application exceptions
  Catch ex As ArgumentException
    'Code to intercept argument exceptions
  Catch ex As BadImageFormatException
    'Code to intercept bad image format exceptions
  Catch ex As FileNotFoundException
    'Code to intercept file not found exceptions
    MsgBox(ex.Message)
  End Try
End Sub

Here, an exception is thrown using the Err object by number (53 = file not found) and the correct exception handler intercepts it and displays the error description by using the ex object.

As you can see, you can trap errors depending on the type of exception too. (Very bad example here!)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Ruffneck,

I didn't know that you can still use the Err object when an Exception is thrown. This is exactly what I am looking for.

Thanks!

-lucyv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top