MapWindow 4 - Plugins : MapWindow Discussion Forum
I wrote a demo plug-in for MapWinGIS. In the process I noticed that the main program is able to catch the exception that I throw in my demo plug-in and pop up a little window to report the exception. So how exactly does MapWinGis do that? If you know it or have a clue,
How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cuipengfei1 ()
Date: March 22, 2010 01:47AM
I wrote a demo plug-in for MapWinGIS.
In the process I noticed that the main program is able to catch the exception that I throw in my demo plug-in and pop up a little window to report the exception.
So how exactly does MapWinGis do that?
If you know it or have a clue, please tell me, thanks!!
In the process I noticed that the main program is able to catch the exception that I throw in my demo plug-in and pop up a little window to report the exception.
So how exactly does MapWinGis do that?
If you know it or have a clue, please tell me, thanks!!
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
bmarch ()
Date: March 22, 2010 08:27AM
Try
{
}
Catch
{
}
{
}
Catch
{
}
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cuipengfei1 ()
Date: March 22, 2010 08:43AM
thank you.
but that is not what I meant.
Let's say I have a Form1 in a WinForm App.
there is a button on Form1, in the button click event's method, I new up another form:
try
{
Form2 form=new Form2();
form.Show();
}
Catch
{}
and in Form2, there is also a button, and that button does nothing but throws an exception.
so, here is the problem, we can not capture the exception thrown by Form2 in Form1's try catch block.
but MapWinGIS obviously did it.
so,the question is how did they do it?
Thank you!
but that is not what I meant.
Let's say I have a Form1 in a WinForm App.
there is a button on Form1, in the button click event's method, I new up another form:
try
{
Form2 form=new Form2();
form.Show();
}
Catch
{}
and in Form2, there is also a button, and that button does nothing but throws an exception.
so, here is the problem, we can not capture the exception thrown by Form2 in Form1's try catch block.
but MapWinGIS obviously did it.
so,the question is how did they do it?
Thank you!
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
bmarch ()
Date: March 22, 2010 02:54PM
In your situation the reason your try catch block isn't catching the exception is because form.Show() dialog returns immediately after you call it. If you called form.showdialog() then the exception in form2 would be caught.
MapWindow catches the exception because its at the class level. Its catching eveything that the plug-in creates. You would need to put a try catch around form 1 to catch exceptions in Form2.
Brian
MapWindow catches the exception because its at the class level. Its catching eveything that the plug-in creates. You would need to put a try catch around form 1 to catch exceptions in Form2.
Brian
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cuipengfei1 ()
Date: March 22, 2010 07:21PM
Hi, bmarch:
Do you mean I need to write code like this in the Main method?
try
{
Application.Run(new Form1());
}
catch
{
MessageBox.Show("Caught!!");
}
I tried this way, it will stop running right after it catch the exception in debug mode.
And if I run it out of VS IDE, it will simply show a warning window in which it says unhandled exception.
Thanks!
Do you mean I need to write code like this in the Main method?
try
{
Application.Run(new Form1());
}
catch
{
MessageBox.Show("Caught!!");
}
I tried this way, it will stop running right after it catch the exception in debug mode.
And if I run it out of VS IDE, it will simply show a warning window in which it says unhandled exception.
Thanks!
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cdegrassi ()
Date: March 22, 2010 07:43PM
Hello cuipengfei1,
bmarch means to say that when you use Form2.Show() the code continues flowing in the button event handler and then exits the method.
So if something happens to form2, it happens outside the "jurisdiction" (scope) of Form1 and of the button event handler.
All these objects are though within the scope of the plugin environment, so the plugin has to intercept the exception.
You have some choices:
1) Replace Form2.show() with Form2.ShowDialog(). This way Form2 "stays" within the scope of the event handler until closed so you can trap unhandled exceptions thrown by Form2
2) Keep Form2.Show. Create a custom error event for form2. Subscribe Form1 to that event. When an exception happens in Form2, route the exception by raising the custom error event. This error will be caught by Form1
3) Keep Form2.Sho Use a Logging Class. All components of your application can subscribe to its events and everytime a component throws an exception, the exception is sent to the Logging Class. All components can then decide what to do with the exception
There are other variations, but there isn't much you can do. You are either "in scope" or "out-of-scope"
I hope this helped.
here are some additional resources, thought there are millions more available.
[msdn.microsoft.com]
[www.csharpfriends.com]
Cheers
Christian Degrassi, MA
Geographer
GIS Analyst/Developer
bmarch means to say that when you use Form2.Show() the code continues flowing in the button event handler and then exits the method.
So if something happens to form2, it happens outside the "jurisdiction" (scope) of Form1 and of the button event handler.
All these objects are though within the scope of the plugin environment, so the plugin has to intercept the exception.
You have some choices:
1) Replace Form2.show() with Form2.ShowDialog(). This way Form2 "stays" within the scope of the event handler until closed so you can trap unhandled exceptions thrown by Form2
2) Keep Form2.Show. Create a custom error event for form2. Subscribe Form1 to that event. When an exception happens in Form2, route the exception by raising the custom error event. This error will be caught by Form1
3) Keep Form2.Sho Use a Logging Class. All components of your application can subscribe to its events and everytime a component throws an exception, the exception is sent to the Logging Class. All components can then decide what to do with the exception
There are other variations, but there isn't much you can do. You are either "in scope" or "out-of-scope"
I hope this helped.
here are some additional resources, thought there are millions more available.
[msdn.microsoft.com]
[www.csharpfriends.com]
Cheers
Christian Degrassi, MA
Geographer
GIS Analyst/Developer
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cuipengfei1 ()
Date: March 22, 2010 09:44PM
Hi, cdegrassi:
I found another way to do it, we just need to add the statements below to the Main method:
Application.ThreadException += (sender, e) => {MessageBox.Show(e.Exception.Message); };
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
then we are able to capture all the exceptions in our App, just like what MapWinGIS can do.
Could this be the way that MapWinGIS uses to do so?
Thanks!
Edited 1 time(s). Last edit at 03/22/2010 09:44PM by cuipengfei1.
I found another way to do it, we just need to add the statements below to the Main method:
Application.ThreadException += (sender, e) => {MessageBox.Show(e.Exception.Message); };
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
then we are able to capture all the exceptions in our App, just like what MapWinGIS can do.
Could this be the way that MapWinGIS uses to do so?
Thanks!
Edited 1 time(s). Last edit at 03/22/2010 09:44PM by cuipengfei1.
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cdegrassi ()
Date: March 23, 2010 12:27AM
cuipengfei1,
in my opinion, the use of Application.ThreadException event leads to mismanagement of errors and it makes way for unmonitored code.
Microsoft suggests to isolate and trap errors through try{} catch{} finally{}. Maybe you need to redesign you application.
If you need to use Application.ThreadException it means your code is not well refactored and designed.
My 2 cents: rethink your code, simple classes, isolate business objects from the interface, use try catch finally when potential errors could arise, monitor user input at the source so errors don't propagate.
Good luck
Cheers
Christian Degrassi, MA
Geographer
GIS Analyst/Developer
in my opinion, the use of Application.ThreadException event leads to mismanagement of errors and it makes way for unmonitored code.
Microsoft suggests to isolate and trap errors through try{} catch{} finally{}. Maybe you need to redesign you application.
If you need to use Application.ThreadException it means your code is not well refactored and designed.
My 2 cents: rethink your code, simple classes, isolate business objects from the interface, use try catch finally when potential errors could arise, monitor user input at the source so errors don't propagate.
Good luck
Cheers
Christian Degrassi, MA
Geographer
GIS Analyst/Developer
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
cuipengfei1 ()
Date: March 23, 2010 02:08AM
Hi cdegrassi,
thank you for your help!
it's a little too complicated now.
I would like to restart from the beginning.
Assume the code below is my plug-in
public class DemoPlugIn : IPlugin
{
//some other code
//.......
public void Initialize(IMapWin MapWin, int ParentHandle)
{
new DemoForm().Show();
}
}
The code below is definition of DemoForm
public partial class DemoForm : Form
{
//some other code
//.......
private void button2_Click(object sender, EventArgs e)
{
throw new Exception();
}
}
I copy the dll file to the installation folder of MapWindow GIS.
When I run MapWindow GIS, my plugin will be loaded, and the first and only thing my plugin will do is to new up an instance of DemoForm and call its Show() method.
Now if I click the button on DemoForm, the Exception will be caught by MapWindow GIS.
So, how does MapWindow GIS do this?
thank you for your help!
it's a little too complicated now.
I would like to restart from the beginning.
Assume the code below is my plug-in
public class DemoPlugIn : IPlugin
{
//some other code
//.......
public void Initialize(IMapWin MapWin, int ParentHandle)
{
new DemoForm().Show();
}
}
The code below is definition of DemoForm
public partial class DemoForm : Form
{
//some other code
//.......
private void button2_Click(object sender, EventArgs e)
{
throw new Exception();
}
}
I copy the dll file to the installation folder of MapWindow GIS.
When I run MapWindow GIS, my plugin will be loaded, and the first and only thing my plugin will do is to new up an instance of DemoForm and call its Show() method.
Now if I click the button on DemoForm, the Exception will be caught by MapWindow GIS.
So, how does MapWindow GIS do this?
Re: How does MapWindow GIS catch exceptions that happen in plugins?
Posted by:
pmeems ()
Date: March 23, 2010 02:23AM
I think you're looking for this piece of code:
It's from the template plug-in. Perhaps it is easier to start with that template and add your code to it.
TemplatePluginVS2008/
Hope it helps,
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
/// <summary>
/// This event is called when a plugin is loaded or turned on in the MapWindow.
/// </summary>
/// <param name="mapWin">The interface to use to access the MapWindow.</param>
/// <param name="parentHandle">The window handle of the main MapWindow form. This handle is useful for
/// making the this.mapWindow the owner of plugin forms.</param>
public void Initialize(MapWindow.Interfaces.IMapWin mapWin, int parentHandle)
{
try
{
this.mapWin = mapWin;
this.parentHandle = parentHandle; // Needed to not let the forms fall behind MapWindow
// Resources:
using (var imageResources = new Resources())
{
this.CreateToolbar(imageResources);
this.CreateMenu(imageResources);
}
}
catch (Exception ex)
{
this.mapWin.ShowErrorDialog(ex, this.reportEmailaddress);
}
}
It's from the template plug-in. Perhaps it is easier to start with that template and add your code to it.
TemplatePluginVS2008/
Hope it helps,
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
Sorry, only registered users may post in this forum.


