How to catching loading exception?

B

Bob

Hi,

I have a simple C# console application, say MyProgram.exe, which uses
classes from MySupport.dll.

If everything are in the same application directory, MyProgram.exe works
fine.

What I would like to know is how to handle, in my program, gracefully when
MySupport.dll is not there?

In a VS development environment, it triggers the JIT debugging. If a
debugger is not selected, it then dump the exception to console.

Any suggestion?

Rover
 
B

Bob

Hi Naveen,

Sorry to disappoint you. The technique in that URL is completely useless to
handle the scenario I have described.

It is only good if you do not have a try/catch block around the most outer
layer. If you have access to code, it is a simple matter to use a try
catch(Exception ).

If you have one of your dependent assembly missing, as described in my
scenario, during loading of the primary app domain, that technique is of no
use. It is fired before it reaches the Main(). That's before you even have a
chance to install your that handler.

You can revisit my initial posting, construct a hello world program which
uses a class, packaged in another assembly to say hello. Removed that
supporting assembly and run your program. Install that unhandled exception
handler as per the bottom of that URL for console app.

For your information, here is the first few lines of the error report (with
the unhandled exception handler installed):
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name
MySupport, or one of its dependencies, was not found.
File name: "MySupport"
at MyProgram.MainProg.Main(String[] args)

=== Pre-bind state information ===
LOG: DisplayName = MySupport, Version=1.0.1874.38372, Culture=neutral,
PublicKey
Token=null
(Fully-specified)
LOG: Appbase =
G:\Projects\Testing\CSharp\TestAssemblyLoading\MyProgram\bin\Debug\
LOG: Initial PrivatePath = NULL
Calling assembly : MyProgram, Version=1.0.1874.38372, Culture=neutral,
PublicKeyToken=null.
===
.........
See where the error reporting points to. MySupport.dll has been deliberately
removed from the AppBase.

Thanks for the tip anyway.

Rover
 
V

VBen

Have you tried, for those external elements, declaring as Object, and then
assigning the value within a try/catch block, using late-binding?
I know early-binding is better performance, but for this kind of problems,
there's no other solution. .NET checks for dependencies before loading, not
on creation, but if you remove the dependency and load elements with
"CreateObject" (or some similar approach), .NET lets you catch that errors.
Another solution might be to use the references inside another assembly
(DLL), using that assembly as a class factory. Since your main app has
control, it can try/catch creation of objects via a class factory.
The main issue is, anyway, that .Net checks that all assembly dependencies
are present prior to executing your assembly, That's why it doesn't even
reach Main().
You could use something like this (it's in VB, but gives you the idea...):
'***Assembly 1 (.exe)
Sub Main()
dim MyObject as Object
try
MyObject = MyFactory.CreateObject()
catch e as Exception
throw(e)
end try
End Sub
'***Assembly 2 (.dll or .exe)
Public Class MyFactory
Public Shared Function CreateObject () As Object
return New MyReferencedClass()
End Function
End Class
'*** END
Of course, assembly 2 requires the reference, but not your main exe...
Hope this helps (and works!!! hehe)
VBen.

Bob said:
Hi Naveen,

Sorry to disappoint you. The technique in that URL is completely useless to
handle the scenario I have described.

It is only good if you do not have a try/catch block around the most outer
layer. If you have access to code, it is a simple matter to use a try
catch(Exception ).

If you have one of your dependent assembly missing, as described in my
scenario, during loading of the primary app domain, that technique is of no
use. It is fired before it reaches the Main(). That's before you even have a
chance to install your that handler.

You can revisit my initial posting, construct a hello world program which
uses a class, packaged in another assembly to say hello. Removed that
supporting assembly and run your program. Install that unhandled exception
handler as per the bottom of that URL for console app.

For your information, here is the first few lines of the error report (with
the unhandled exception handler installed):
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name
MySupport, or one of its dependencies, was not found.
File name: "MySupport"
at MyProgram.MainProg.Main(String[] args)

=== Pre-bind state information ===
LOG: DisplayName = MySupport, Version=1.0.1874.38372, Culture=neutral,
PublicKey
Token=null
(Fully-specified)
LOG: Appbase =
G:\Projects\Testing\CSharp\TestAssemblyLoading\MyProgram\bin\Debug\
LOG: Initial PrivatePath = NULL
Calling assembly : MyProgram, Version=1.0.1874.38372, Culture=neutral,
PublicKeyToken=null.
===
........
See where the error reporting points to. MySupport.dll has been deliberately
removed from the AppBase.

Thanks for the tip anyway.

Rover

Naveen said:
Hi..

You can write a log file where it can be dumped...else popup a message box
to catch those...

This link might help you
further...http://www.codeproject.com/dotnet/unhandledexceptions.asp

With Best Regards
Naveen K S
 
B

Bob

I guess if you place all your Main() code in a separate AppDomain instead of
the primary,
you could intercept this.

But this would be much easier.

The other way is to host one's CLR via Cor API.

I would have thought CLR default loader would have a way to allow one to
intercept this early exception.

Rover

VBen said:
Have you tried, for those external elements, declaring as Object, and then
assigning the value within a try/catch block, using late-binding?
I know early-binding is better performance, but for this kind of problems,
there's no other solution. .NET checks for dependencies before loading,
not
on creation, but if you remove the dependency and load elements with
"CreateObject" (or some similar approach), .NET lets you catch that
errors.
Another solution might be to use the references inside another assembly
(DLL), using that assembly as a class factory. Since your main app has
control, it can try/catch creation of objects via a class factory.
The main issue is, anyway, that .Net checks that all assembly dependencies
are present prior to executing your assembly, That's why it doesn't even
reach Main().
You could use something like this (it's in VB, but gives you the idea...):
'***Assembly 1 (.exe)
Sub Main()
dim MyObject as Object
try
MyObject = MyFactory.CreateObject()
catch e as Exception
throw(e)
end try
End Sub
'***Assembly 2 (.dll or .exe)
Public Class MyFactory
Public Shared Function CreateObject () As Object
return New MyReferencedClass()
End Function
End Class
'*** END
Of course, assembly 2 requires the reference, but not your main exe...
Hope this helps (and works!!! hehe)
VBen.

"Bob" <[email protected]> escribió en el
mensaje
Hi Naveen,

Sorry to disappoint you. The technique in that URL is completely useless to
handle the scenario I have described.

It is only good if you do not have a try/catch block around the most
outer
layer. If you have access to code, it is a simple matter to use a try
catch(Exception ).

If you have one of your dependent assembly missing, as described in my
scenario, during loading of the primary app domain, that technique is of no
use. It is fired before it reaches the Main(). That's before you even
have a
chance to install your that handler.

You can revisit my initial posting, construct a hello world program which
uses a class, packaged in another assembly to say hello. Removed that
supporting assembly and run your program. Install that unhandled
exception
handler as per the bottom of that URL for console app.

For your information, here is the first few lines of the error report (with
the unhandled exception handler installed):
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name
MySupport, or one of its dependencies, was not found.
File name: "MySupport"
at MyProgram.MainProg.Main(String[] args)

=== Pre-bind state information ===
LOG: DisplayName = MySupport, Version=1.0.1874.38372, Culture=neutral,
PublicKey
Token=null
(Fully-specified)
LOG: Appbase =
G:\Projects\Testing\CSharp\TestAssemblyLoading\MyProgram\bin\Debug\
LOG: Initial PrivatePath = NULL
Calling assembly : MyProgram, Version=1.0.1874.38372, Culture=neutral,
PublicKeyToken=null.
===
........
See where the error reporting points to. MySupport.dll has been deliberately
removed from the AppBase.

Thanks for the tip anyway.

Rover

Naveen said:
Hi..

You can write a log file where it can be dumped...else popup a message box
to catch those...

This link might help you
further...http://www.codeproject.com/dotnet/unhandledexceptions.asp

With Best Regards
Naveen K S


:

Hi,

I have a simple C# console application, say MyProgram.exe, which uses
classes from MySupport.dll.

If everything are in the same application directory, MyProgram.exe works
fine.

What I would like to know is how to handle, in my program, gracefully
when
MySupport.dll is not there?

In a VS development environment, it triggers the JIT debugging. If a
debugger is not selected, it then dump the exception to console.

Any suggestion?

Rover
 
B

Bob

VBen,

In fact one needs not create a new AppDomain to do this. All one needs is a
launcher of the appropriate type. For instance a console app, all you have
to do is

private static void Launch( string[] args )
{
string progName = args[0];
string[] theRest = new string[ args.Length - 1 ];
string.Copy( args, 1, theRest, 0, args.Length - 1];
try
{
AppDomain.CurrentAppDomain.ExecuteAssembly( progName, null,
theRest );
}
catch( Exception e )
{
Console.WriteLine( "Loading exception:\n{0}", e.Message );
}
}

Did a test on my scenario and it works!! I left out the customary error
handling code for brevity.

Bob

VBen said:
Have you tried, for those external elements, declaring as Object, and then
assigning the value within a try/catch block, using late-binding?
I know early-binding is better performance, but for this kind of problems,
there's no other solution. .NET checks for dependencies before loading,
not
on creation, but if you remove the dependency and load elements with
"CreateObject" (or some similar approach), .NET lets you catch that
errors.
Another solution might be to use the references inside another assembly
(DLL), using that assembly as a class factory. Since your main app has
control, it can try/catch creation of objects via a class factory.
The main issue is, anyway, that .Net checks that all assembly dependencies
are present prior to executing your assembly, That's why it doesn't even
reach Main().
You could use something like this (it's in VB, but gives you the idea...):
'***Assembly 1 (.exe)
Sub Main()
dim MyObject as Object
try
MyObject = MyFactory.CreateObject()
catch e as Exception
throw(e)
end try
End Sub
'***Assembly 2 (.dll or .exe)
Public Class MyFactory
Public Shared Function CreateObject () As Object
return New MyReferencedClass()
End Function
End Class
'*** END
Of course, assembly 2 requires the reference, but not your main exe...
Hope this helps (and works!!! hehe)
VBen.

"Bob" <[email protected]> escribió en el
mensaje
Hi Naveen,

Sorry to disappoint you. The technique in that URL is completely useless to
handle the scenario I have described.

It is only good if you do not have a try/catch block around the most
outer
layer. If you have access to code, it is a simple matter to use a try
catch(Exception ).

If you have one of your dependent assembly missing, as described in my
scenario, during loading of the primary app domain, that technique is of no
use. It is fired before it reaches the Main(). That's before you even
have a
chance to install your that handler.

You can revisit my initial posting, construct a hello world program which
uses a class, packaged in another assembly to say hello. Removed that
supporting assembly and run your program. Install that unhandled
exception
handler as per the bottom of that URL for console app.

For your information, here is the first few lines of the error report (with
the unhandled exception handler installed):
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name
MySupport, or one of its dependencies, was not found.
File name: "MySupport"
at MyProgram.MainProg.Main(String[] args)

=== Pre-bind state information ===
LOG: DisplayName = MySupport, Version=1.0.1874.38372, Culture=neutral,
PublicKey
Token=null
(Fully-specified)
LOG: Appbase =
G:\Projects\Testing\CSharp\TestAssemblyLoading\MyProgram\bin\Debug\
LOG: Initial PrivatePath = NULL
Calling assembly : MyProgram, Version=1.0.1874.38372, Culture=neutral,
PublicKeyToken=null.
===
........
See where the error reporting points to. MySupport.dll has been deliberately
removed from the AppBase.

Thanks for the tip anyway.

Rover

Naveen said:
Hi..

You can write a log file where it can be dumped...else popup a message box
to catch those...

This link might help you
further...http://www.codeproject.com/dotnet/unhandledexceptions.asp

With Best Regards
Naveen K S


:

Hi,

I have a simple C# console application, say MyProgram.exe, which uses
classes from MySupport.dll.

If everything are in the same application directory, MyProgram.exe works
fine.

What I would like to know is how to handle, in my program, gracefully
when
MySupport.dll is not there?

In a VS development environment, it triggers the JIT debugging. If a
debugger is not selected, it then dump the exception to console.

Any suggestion?

Rover
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top