Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I already know how to throw an exception with a custom message. I also know how to try/catch exceptions. What I need is a little different.

 

I need to be able to catch an exception ONLY to pass a message to the user via a messagebox then let the normal handling of the exception continue (i.e. shutting down the program). I don't want to have to shut down the program programmatically.

 

Is this possible?

 

I have a sneaking suspicion it might involve subclassing the System.Exception class to make a custom exception routine. Hoping there might be a slightly easier way.

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

  • Administrators
Posted (edited)

Exceptions aren't designed to be used in this way - they are a way to signal an error not just as a means of passing a message.

 

You probably want to look at using / raising events as they would be far more suitable in this scenario.

 

Alternatively you might want to look at handling the AppDomain.CurrentDomain.UnhandledException event.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
You could just display the message from the exception and then rethrow the exception.
try
{
// something that will throw an exception
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw ex;
}

An unhandled exception should terminate the program.

Posted

And if you want to use the try catch method

 

Try

catch ex as exception

finally
' Processes next step after handling the error
end try

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted
Oh ok, I went back and read what he was looking for a third time. I see hwta he is looing for now. Sorry, brain is a little fried. Been working on this project trying to get something to work for thepast 2 days.
Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

here is a way in VB. Not sure what you are lookng for

On Error Goto ErrorHandle
'code here
exit sub
ErrorHandle:
messagebox.show("BLABLAH")
Resume Next

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted
Well, that's still not going to do it. In VB it is pretty much the same:
       Try

           ' do something that might throw an exception

       Catch ex As Exception

           messagebox.show(ex.Message)

           Throw ex

       End Try

Posted

Thanks PlausiblyDamp and all that have answered. I supposed I better get a little more general AND a little more specific, and explain what I am wanting to do and why.

 

What I was trying to do is find a way that I can shut down a program without having to use the standard programmatic methods for reasons of obfuscation. Just throwing an exception of course will not do it, and I have come to realize that. My concern is disassembly of my product... it would be easy to search in MSIL code or even reconstituted VB/C# .Net for the standard methods of shutting down programmatically, and then remove them.

 

My thought, although erroneous, was using an error (i.e. a sneaky divide-by-zero) to cause an exception, pass my custom message saying they didn't have the right registration info, then let the exception shut it down.

 

One of the things I realized though in testing after I posted, was that:

 

  1. At least the common exceptions like that don't shut down the program as they used to even if uncaught.
  2. Even injecting your custom message will result in a very unprofessional looking exception dialog.
  3. It just plain isn't going to work.

That being said, I would be open to any suggestions that might accomplish the goal of shutting down the program or DLL assembly without easily traced and removed code. Personally though I am beginning to think that this is going to be just an instance of "nothing you can really do". Seems that intellectual property security is not a big advantage and actually more of a liability with .NET

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

The bottom line is that if somebody wants to break your licensing algorithm -- they will, even if your application is written in C or straight Assembly. Granted, with a managed platform like .NET, decompiling the assembly is vastly easier. There are tools available to obfuscate your assemblies.

 

Check out this article: http://www.yoda.arachsys.com/csharp/obfuscation.html

 

I basically agree with everything that is said there.

Posted (edited)

Hadn't read that article before, but read a few like it. I agree also. I wasn't attempting to make a foolproof program, just a more difficult one. Just because it isn't easy to obfuscate your methods in .NET doesn't mean you need to serve it on a silver platter. :p

 

All in all, I am not worried about most people getting ahold of my code. But I just don't want to make it easy on them to do so. If I can make them lose more than two hairs in frustration when they try, I feel I have accomplished my purpose. ;)

 

The assembly isn't going to be world shattering, just extremely useful to project developers. It is a plugin manager control. Drop it on your form, set a few properties, like plugin directory, and interface libraries, and it will load your plugins AND provide a plugin manager control that you can add to your program. You will be able to have managed and unmanaged plugins. That simply means that plugins that use the built in IPlugInfo interface will be managed, those that don't won't be managed by the manager.

 

Managed plugins can have startup properties set (start at runtime, etc) so the end user of the program can decide which ones they want running. Unmanaged plugins will always run at runtime and don't show in the manager at all, so that is good for core application code.

 

The overall goal is to provide a reusuable control that will make plugins a simple operation for the developer.

Edited by mooman_fl

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...