How to catching loading exception?

Discussion in 'ASP .Net' started by Bob, Feb 16, 2005.

  1. Bob

    Bob Guest

    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
    Bob, Feb 16, 2005
    #1
    1. Advertising

  2. 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


    "Bob" wrote:

    > 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
    >
    >
    >
    =?Utf-8?B?TmF2ZWVu?=, Feb 17, 2005
    #2
    1. Advertising

  3. Bob

    Bob Guest

    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" <> wrote in message
    news:...
    >
    > 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
    >
    >
    > "Bob" wrote:
    >
    >> 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
    >>
    >>
    >>
    Bob, Feb 17, 2005
    #3
  4. Bob

    VBen Guest

    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" <> escribió en el mensaje
    news:#...
    > 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" <> wrote in message
    > news:...
    > >
    > > 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
    > >
    > >
    > > "Bob" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >>

    >
    >
    VBen, Feb 18, 2005
    #4
  5. Bob

    Bob Guest

    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" <> wrote in message
    news:%...
    > 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" <> escribió en el
    > mensaje
    > news:#...
    >> 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" <> wrote in message
    >> news:...
    >> >
    >> > 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
    >> >
    >> >
    >> > "Bob" wrote:
    >> >
    >> >> 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
    >> >>
    >> >>
    >> >>

    >>
    >>

    >
    >
    Bob, Feb 19, 2005
    #5
  6. Bob

    Bob Guest

    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" <> wrote in message
    news:%...
    > 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" <> escribió en el
    > mensaje
    > news:#...
    >> 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" <> wrote in message
    >> news:...
    >> >
    >> > 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
    >> >
    >> >
    >> > "Bob" wrote:
    >> >
    >> >> 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
    >> >>
    >> >>
    >> >>

    >>
    >>

    >
    >
    Bob, Feb 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Hayes
    Replies:
    8
    Views:
    9,326
    DDaanV
    Oct 21, 2010
  2. =?Utf-8?B?Y2xpY2tvbg==?=

    Catching a Constraint Exception

    =?Utf-8?B?Y2xpY2tvbg==?=, Mar 29, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    4,235
    =?Utf-8?B?Y2xpY2tvbg==?=
    Mar 29, 2006
  3. Daniel
    Replies:
    8
    Views:
    5,733
    Daniel
    Jan 24, 2005
  4. Bryan Bullard
    Replies:
    17
    Views:
    566
    Donovan Rebbechi
    Jan 11, 2004
  5. Dave

    Catching class exception

    Dave, Jan 27, 2005, in forum: C++
    Replies:
    11
    Views:
    536
    BigBrian
    Jan 28, 2005
Loading...

Share This Page