Handling missing dll exception

K

kimiraikkonen

Hi,
I have an mini application with a dll named classlibrary1.

I used it via imports statement, but i tried something to catch a
custom msgbox error if the dll is not found or bad for the user.

I tried catch blabla as dllnotfoundexception with no help. Also try-
catch couldn't handle missing dll exception.

Could you define a sample code which calls a simple msgbox when a
button is pressed (if dll is missing)?

thanks
 
P

Phill W.

kimiraikkonen said:
I have an mini application with a dll named classlibrary1.
I used it via imports statement,

.... (you referenced it first, but anyway) ...
but i tried something to catch a custom msgbox error if the dll
is not found or bad for the user.

Where are you trying to catch this error?

..Net code is loaded and linked method-by-method so, as soon as you try
to /call/ a method that uses your dll and it's not there, Boom! That
method fails to "appear", and you get a TypeLoadException (usually).

/If/ you can wrap a Try .. Catch around the call to /that/ routine, you
stand a chance:

Try
LoadCL1()
Catch ex As TypeLoadException
' Oops
End Try

Sub LoadCL1()

Dim olib as New classlibrary1()
. . .

End Sub

This /won't/ work ...

Sub LoadCL2()
Try
Dim olib as New classlibrary1()
. . .
Catch ex As TypeLoadException
' Oops
End Try
End Sub

.... because the /whole method/ gets loaded in one go and the referenced
libraries, etc. loaded. The code fails /before/ it even starts to
execute your "Try".
(BTW, this has caused me /great/ confusion in the past where the method
that's failing to load is the Elapsed event handler on a Timer!).

HTH,
Phill W.
 
K

kimiraikkonen

... (you referenced it first, but anyway) ...


Where are you trying to catch this error?

.Net code is loaded and linked method-by-method so, as soon as you try
to /call/ a method that uses your dll and it's not there, Boom! That
method fails to "appear", and you get a TypeLoadException (usually).

/If/ you can wrap a Try .. Catch around the call to /that/ routine, you
stand a chance:

Try
LoadCL1()
Catch ex As TypeLoadException
' Oops
End Try

Sub LoadCL1()

Dim olib as New classlibrary1()
. . .

End Sub

This /won't/ work ...

Sub LoadCL2()
Try
Dim olib as New classlibrary1()
. . .
Catch ex As TypeLoadException
' Oops
End Try
End Sub

... because the /whole method/ gets loaded in one go and the referenced
libraries, etc. loaded. The code fails /before/ it even starts to
execute your "Try".
(BTW, this has caused me /great/ confusion in the past where the method
that's failing to load is the Elapsed event handler on a Timer!).

HTH,
Phill W.

I used catch inside button1.click sub. No luck. Then i tried inside
try-catch inside class library (dll) no luck again. Then tried "catch
xx as dllnotfoundexception" inside button1.click sub with no luck.

Could you simplify and sample with a more understanding way?

Very thanks.
 
P

Phill W.

kimiraikkonen said:
I used catch inside button1.click sub. No luck. Then i tried inside
try-catch inside class library (dll) no luck again. Then tried "catch
xx as dllnotfoundexception" inside button1.click sub with no luck.
Could you simplify and sample with a more understanding way?

The TypeLoadException (or whatever) happens because the Framework is
trying to load and link your method and resolve the Types it needs in
order to run it. You /can't/ catch this error inside the method,
because the method never gets properly loaded, so your code can't run.

Try
LoadCL1()
<< Here, the framework tries to load and link your method
<< called LoadCL1(). If the dll it needs is missing, this
<< fails and you get an Exception...
Catch ex As TypeLoadException
' Oops
<< ... that you catch and handle here
End Try

Sub LoadCL1()
<< If the dll is missing, the framework can't load this method
<< so you can't catch the error here
Dim olib as New classlibrary1()
End Sub

You can watch this happening in the IDE is you have the Modules window
open and step through the code - between the call to LoadCl1 and the IDE
stepping into the start of the method, you'll see the dll being loaded.

HTH,
Phill W.
 
A

Armin Zingler

In addition to Phill,

you can put the Try-Catch at top level in each thread's main procedure:

shared sub main
try
MainInternal
catch
...
end try
end sub

I put your actual sub Main code in sub MainInternal in order to avoid any
type usage in sub Main. Otherwise not even sub Main might be started because
of a missing dll.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top