Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

How to handle Argument Exception not handled error

I have this statement:  throw new System.ArgumentException("test throw");

I am getting a message: Argument Exception was unhandled by user code.

How do I fix this?  (I googled for it, but didn't find what I wanted).
0
rmmarsh
Asked:
rmmarsh
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
Wrap the calling code in a try/catch:

try
{
    YourFunction();
}
catch (ArgumentException ex)
{
    Console.WriteLine(ex.ToString());
}

Open in new window

0
 
rmmarshAuthor Commented:
Hi kaufmed (what's your real first name?  mine's Rolf)... since I seem to be luck enough to get you most of the time, might as well introduce ourselves! :D )

What I want to do is throw an exception, depending on where I am in the program, and have a handler that will allow me to gather more information, such as stack trace, etc.
0
 
käµfm³d 👽Commented:
Hi kaufmed (what's your real first name?  mine's Rolf)
Awww...  and I was so used to my internet anonymity   ; )

My name is Kenneth. Glad to make your acquaintance!

since I seem to be luck enough to get you most of the time
That probably just means I'm spending way to much time on here  = D

What I want to do is throw an exception, depending on where I am in the program, and have a handler that will allow me to gather more information, such as stack trace, etc.
Understandable. That is what you will get with a try/catch. The thing you have to determine is where you want to collect this information. There are those that believe you should handle an exception as close as possible to the place where it occurred; there are others who believe you should have a more "global" handler that captures most/all of your exceptions. I think it depends on what your application is doing at the time. For a forms app, having a handler close to the exception might be more acceptable because you have more immediate interaction with your user. For a web app, having a global handler might be more desirable because the web was designed to be stateless, so recovering from an exception doesn't grant you much in terms of rescuing the state of your application, and it may come down to telling your user that there was an error and they cannot continue.

I believe you have a forms app, yes?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
rmmarshAuthor Commented:
Yes, it's a forms app... since exceptions can happen anywhere, I have try/catch blocks where I need to attempt a recovery... but, there are times when I want to cause an exception so I can get the stack trace, OS, etc.  For that, I like to throw an ArgumentException, with some kind of identifier, and let the app crash, since I don't know what caused it.  It's nice in that I can throw the exception anywhere and have a central place to catch it.

Sometimes I email the exception data to me (kinda like what MS does)... easier than asking the user what happened and getting a two-word answer.  My license states that this may happen, so there are no surprises to the user.  It's all done in the background, and having done this for a few years now, it works like a champ.
0
 
käµfm³d 👽Commented:
You should be able to have a "global" handler for this purpose. There is also the UnhandledException event which you can handle. This event *should* receive any unhandled exception in your code prior to passing it off to the operating system. Handling this event, you can prevent your application from giving a nasty, non-user-friendly message. I posted an example here:  http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26367277.html#33322318
0
 
rmmarshAuthor Commented:
Thanks, Kenneth...
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now