Try...Catch across assemblies

G

Guest

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
 
J

Jon Skeet [C# MVP]

Bryce Covert said:
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?

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

Guest

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!
 
M

Mattias Sjögren

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
 
G

Guest

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!
 
J

Jon Skeet [C# MVP]

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);
}
}
}
 
M

Mehdi

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:

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

Jon Skeet [C# MVP]

Mehdi said:
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.

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

Mehdi

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

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.
 

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