Main Site Documentation

Try Catch Exceptions ! NO REALLY Catch them


#1

I noticed that the Try{}Catch doesn’t work all the time… i mean some exceptions just refuse to bubble up and no matter how many try catch you place around the offending code, it just refuses to catch it… is it by design issue, or is it that we should really take the verbatim meaning of the WORD TRY… where it tries and fails sometimes???

maybe that why they call it try catch and not DO Catch ;D

Jay.


#2

Some exceptions really cant be catched
a StackOverflowException for example.
If youre getting this you might have some design issues in your code architecture :slight_smile:

What type of exception do you get?


#3

thanks for your reply…
here is a piece of code that will give you an exception that you can never catch… heck it will even CRASH VS2010, when you debug the application using F5…

this code shouldn’t crash vs2010 and i should be able to catch that exception… because it is a valid code where i could get a call with an invalid method name…


        object InvokeMethod(string methodName, params object[] args)
        {
            Debug.Print("**********************About to execute this Method=" + methodName);
            try
            {
                var methodInfo = GetType().GetMethod(methodName);

                return methodInfo.Invoke(this, args);
            }
            catch (Exception)
            {
                Debug.Print("Error invoking");
            }
            return null;
        }

  public void TestMethod()
        {
            Debug.Print("this is a test");
        }

//now call it like this somewhere in your code:

InvokeMethod("TestMethod");// this will work..

Thread.Sleep(2000);

InvokeMethod("TestMethod",new object[]{"test"});// this will Crash VS2010, because you are passing arguments to a method that doesn't accept any... it shouldn't crash but instead it should trow the exception.

InvokeMethod("UnknowMehod");// this will Crash since you are calling a non existing method... again it should safely land in the Catch but instead it crashes...


Or am i expecting too much???

Enjoy!!


#4

No, you’re not expecting too much. In this case, I suspect a bug in NETMF. I’d file it on their site.


#5

+1 for the bug
should throw a ArgumentException…


#6

If you file a bug at netmf, let us know so we can vote it up for you. The team has promised to fix all bugs for the 4.3 release (beta in June).


#7

Thank you for the confirmation ad the suggestions:
bug filed…
http://netmf.codeplex.com/workitem/1598

thank you guys…


#8

Jay,

Did you try adding an exception variable, like this:

try
{
    // do something
}
catch (Exception e)
{
    // catch something
}


#9

Hi jasdev,
I doubt it would make a difference, since that only return the exception data, which is useless in the current NETMF anyways.


#10

Jay,

I know that your syntax is allowed, but I thought maybe the NETMF implementation is having a problem with it. You could also try a catch statement without a parameter just to see what happens.