How to Catch exception from DLL

hirenlad

New member
Joined
Sep 8, 2011
Messages
3
Programming Experience
1-3
I've written a DLL that contains a bunch of classes. One of my methods validates the contents of a file and throws any necessary exceptions based on invalid content - I'm using a standard "throw new exception..." call ...the thing is, when my code reaches the line where the exception is thrown, my Visual Studio IDE hangs up on that line instead of the external call to my validate method.

A short conceptual example to demonstrate:

MyDLL content:

Public Class FileContent
Public Function ValidateFile(ByVal FileName As String) As Boolean
If Not System.IO.File.Exists(FileName) Then
Throw New Exception("File "& FileName & " doesn't exist.")
End If
End Function
End Class

Second project that contains reference to DLL

Imports MyDLL
Public Module Module1
Sub Main()
Dim oFC As New FileContent
oFC.ValidateFile("C:\FileThatDoesntExist.txt")
End Sub
End Module

So my IDE is actually pulling up the VB file that contains the DLL content and highlights the line in the ValidateFile instead of just hanging on the oFC.ValidateFile method line where the exception is thrown instead of the IDE hanging on the oFC.ValidateFile line in my calling application.

I just want user to have access to the any exceptions that are raised. Here if "C:\FileThatDoesntExist.txt" file does not exist then Dll throw exception which should be catch through Application.ThreadException.. How to Catch exception from DLL ?

-- Hiren Lad
 

hirenlad

New member
Joined
Sep 8, 2011
Messages
3
Programming Experience
1-3
Hello JohnH,
You did not get my question very well. I have two project, one make generate DLL and another project in added generated dll reference, So how can i get exception from DLL into main application ???

Thank you.
-- Hiren Lad
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,454
Location
Norway
Programming Experience
10+
The exception does get to the calling code, and when unhandled the debugger will look up the source if available, which in your case since you sit on the source it is. If other parties were to use your release mode dll they would not see that source. If you Try-Catch the call you will see the caller does indeed get and catch the exception. If you want to hide your own code from your own debugging you can apply debugger attributes such as DebuggerNonUserCode, DebuggerStepThrough or DebuggerHidden to the class or method.
 
Top Bottom