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 / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Exception Handling...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jacek Jurkowski - 22 Feb 2008 11:22 GMT
Why in thet case "throw" doesn't work?
There is no exception rising and there should
be!

public class Aaa
{
   public static void Main()
   {
       try
       {
           Bbb bbb = new Bbb();
           bbb.ShowDialog();
       }
       catch(Exception e)
       {
           MessageBox.Show(e.Message);
       }
   }
}

public class Bbb : Form
{
   protected override OnLoad(EventArgs e)
   {
       base.OnLoad(e);
       throw new Exception("exception");
   }
}
Marc Gravell - 22 Feb 2008 11:46 GMT
It works just fine for me... what are you expecting? And what version of
.NET are you using?

Load happens as the form is preparing to display (i.e. during ShowDialog,
not during the constructor); this throws your exception, which is shown via
the MessageBox...?

Of course, since your code doesn't compile "as is" (the missing "void"), I
wonder if the real problem has been left in the /real/ code, and not posted
in the sample?

Marc
Jon Skeet [C# MVP] - 22 Feb 2008 11:56 GMT
> It works just fine for me... what are you expecting? And what version of
> .NET are you using?
[quoted text clipped - 6 lines]
> wonder if the real problem has been left in the /real/ code, and not posted
> in the sample?

On my machine it's not shown in the deliberate (clean) message box -
it's shown as a nasty ".NET has failed" error. Not entirely sure what's
going on there, but it may be due to ShowDialog being shown from a non-
UI thread.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 22 Feb 2008 12:19 GMT
Very curious!

Could try adding an [STAThread]; also might depend on the OS; I'm using XP /
x86; tested fine via 2008 team and express.

Marc
Jon Skeet [C# MVP] - 22 Feb 2008 12:26 GMT
> Very curious!
>
> Could try adding an [STAThread]; also might depend on the OS; I'm using XP /
> x86; tested fine via 2008 team and express.

Vista, running from a console. Adding [STAThread] makes no odds, nor
does compiling as a Windows Forms app instead of a console app.

It then shows the form, by the way...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 22 Feb 2008 12:28 GMT
Actually I lied - it works with a debugger attached, but not standalone. The
worst type of bug... I'll keep looking...
Marc Gravell - 22 Feb 2008 12:53 GMT
Interesting...

First: if I subscribe to Application.ThreadException, then the behavior is
identical with/without debugger.

Second: with the debugger, the stack-trace goes all the way back to Main();
without the debugger, the stack-trace only goes back as far as
NativeWindow.Callback [even though ManagedThreadId is the same], which
explains why it is considered unhandled, and why it appears as a
ThreadException.

Actually sounds similar to something I was looking at the other day where
the principal evaporated for the duration of an async callback /
Control.Invoke, but was there (again: on the same ManagedThreadId)
subsequently...

Marc
Jacek Jurkowski - 22 Feb 2008 12:25 GMT
Messagebox isn't shown with OnLoad but it
works fine with OnShown. Its a problem for me because
i used to do some actions in OnLoad and if some of that
actions go wrong i have no any exception message any more.

Vs.Net 2008 NetFramework 3.5

Here is a code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   static class Program
   {
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);

           try
           {
               Form1 f = new Form1();
               Application.Run(f);
           }
           catch (Exception c)
           {
               MessageBox.Show(c.Message);
           }
       }
   }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }
       protected override void OnLoad(EventArgs e)
       {
           base.OnLoad(e);

           throw new Exception("sjhsjshsj");
       }

       protected override void OnShown(EventArgs e)
       {
           base.OnShown(e);

           //throw new Exception("sjhsjshsj");
       }
   }
}
Ignacio Machin ( .NET/ C# MVP ) - 22 Feb 2008 12:37 GMT
Hi,

> Why in thet case "throw" doesn't work?
> There is no exception rising and there should
> be!

Are you saying there is no error at all?

Do you get the form displayed?
Jacek Jurkowski - 22 Feb 2008 12:52 GMT
No error, no window at all ...
Jacek Jurkowski - 22 Feb 2008 12:39 GMT
B.T.W

On many form on my application  i must do
some actions on startup. F.e. i have to fill
a list of clients or  a products. I used to put that
code in OnLoad of the form mainly. Where do you do
such actions mostly?

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.