Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

Try...Catch across assemblies

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bryce Covert - 29 Jun 2005 17:22 GMT
Hi,

I have an application that loads an assembly via reflection, and then runs a
single form on that form. That's basically all the application does. Enclosed
in this 3-line statement is a try...catch so that if any exceptions are
thrown, the application will pick them up. This works fine if launched from
Visual Studio, however, it doesn't catch any of these exceptions (in Debug or
Release) outside of Visual Studio.

Is it possible to get this to work in any way?

Thanks!

Bryce
Jon Skeet [C# MVP] - 29 Jun 2005 17:49 GMT
> I have an application that loads an assembly via reflection, and then runs a
> single form on that form. That's basically all the application does. Enclosed
[quoted text clipped - 4 lines]
>
> Is it possible to get this to work in any way?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Bryce Covert - 29 Jun 2005 18:49 GMT
Class Library:

Public Class Class1
   Inherits System.Windows.Forms.Form
   Public Sub New()

   End Sub

   Private Sub Class1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
       Throw New Exception("error.")

   End Sub

   Private Sub InitializeComponent()
       '
       'Class1
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Name = "Class1"

   End Sub
End Class

Application: (Reference the class library)
Module Module1

   Sub Main()

       Try

           Dim CoreAssembly As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom("ExceptionClass.DLL")

           Dim classinstance As System.Windows.Forms.Form =
CoreAssembly.CreateInstance("ExceptionClass.Class1", False)

           classinstance.ShowDialog()

       Catch ex As System.Exception

           System.Windows.Forms.MessageBox.Show(ex.Message)
       End Try

   End Sub

End Module

...

With this code, you will get a messagebox showing the error when run from
VS. You will not from running outside.

Thanks!
Mattias Sjögren - 30 Jun 2005 08:03 GMT
Bryce,

>With this code, you will get a messagebox showing the error when run from
>VS. You will not from running outside.

The exception gets caught alright here, running on v1.1 of the
framework.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Bryce Covert - 30 Jun 2005 15:19 GMT
Really? Odd that it doesn't for me. Running straight from the .exe, I get the
exception in the "unhandled exception" messagebox.

I'm running Visual Studio .NET 2003

Any help anyone?

Thanks!
Jon Skeet [C# MVP] - 01 Jul 2005 07:15 GMT
<snip>

> With this code, you will get a messagebox showing the error when run from
> VS. You will not from running outside.

Well, the good news is that it's got nothing to do with running in a
different assembly. The bad news is I don't entirely understand it. I
*believe* it's because when you show a dialog, it starts a new message
pump, and *that's* what is popping up the message box. I don't know how
to solve it though. It might be worth asking in the Windows Forms
newsgroup. Here's the code I used to reproduce it in the end in a more
compact (and readable to me, as a C# programmer :) form:

using System;
using System.Windows.Forms;

public class Test : Form
{
   public Test()
   {
       Load += new EventHandler(LoadEventHandler);
   }
   
   private void LoadEventHandler(object sender, EventArgs args)
   {
       throw new Exception("Hello");
   }
   
   static void Main()
   {
       try
       {
           new Test().ShowDialog();
       }
       catch (Exception e)
       {
           Console.WriteLine ("Caught "+e);
       }
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mehdi - 01 Jul 2005 22:24 GMT
> Well, the good news is that it's got nothing to do with running in a
> different assembly. The bad news is I don't entirely understand it. I
[quoted text clipped - 3 lines]
> newsgroup. Here's the code I used to reproduce it in the end in a more
> compact (and readable to me, as a C# programmer :) form:

Funny, reading your code i've realized that this is the exact same problem
i ran into last week. Throwing an exception from the Load event handler
results in a weird behaviour in Visual Studio (unhandled exception even
with a try/catch) but runs fine outside VS. So i just assumed that it was a
bug with VS and moved on. But i'd be interested to know what the real
problem is.
Jon Skeet [C# MVP] - 01 Jul 2005 23:51 GMT
> > Well, the good news is that it's got nothing to do with running in a
> > different assembly. The bad news is I don't entirely understand it. I
[quoted text clipped - 10 lines]
> bug with VS and moved on. But i'd be interested to know what the real
> problem is.

That's the opposite behaviour from what's being reported here. I
haven't even run my test program in VS - it's running it from the
command line that I get the message box, and I believe it's running
outside the debugger that the OP is getting problems. Hmm...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mehdi - 02 Jul 2005 02:02 GMT
>> Funny, reading your code i've realized that this is the exact same problem
>> i ran into last week. Throwing an exception from the Load event handler
[quoted text clipped - 7 lines]
> command line that I get the message box, and I believe it's running
> outside the debugger that the OP is getting problems. Hmm...

Right, i misread the post. Well, in this case, the behaviour i had was
effecively the exact opposite. I'll try to see tomorrow if i kept this bit
of code somewhere in some test project.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.