Exception weirdness in VS 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CharlesA

    Exception weirdness in VS 2005

    Hi folks,
    I'm working on code from Herb Schildt's book in which he says that an
    exception can be thrown by one method and caught by another

    class ExcTest
    {
    public static void genException()
    {
    int[] nums = new int[4];
    Console.WriteLi ne("Before exception is generated");
    //generate an index out-of-bounds exception
    for (int i = 0; i < 10; i++)
    {
    nums[i] = i;
    Console.WriteLi ne("nums[{0}] : {i}", i, nums[i]);
    }

    Console.WriteLi ne("this won't be displayed");
    }
    }

    class ExcDemo2
    {
    public static void Main()
    {
    try
    {
    ExcTest.genExce ption();

    }
    catch (IndexOutOfRang eException)
    {
    //catch the exception
    Console.WriteLi ne("Index out-of-bounds!");
    }
    Console.WriteLi ne("After catch statement.");
    }
    }

    what's supposed to happen is that calling ExcTest.genExce ption() will
    generate the exception in genExcetpion (which has no exception handling) and
    so will therefore be handled in the caller Main() which does!

    however all I get is Visual studio 2005 intruding into this scenario with an
    over-detailed exception dialog box that highlights the offending line in
    yellow and goes into debug mode.

    is there a way I can get Main() to handle to the exception without having
    VStudio intrude, is there some setting somewhere?

    Regards, and thanks in advance,
    CharlesA

  • Morten Wennevik

    #2
    Re: Exception weirdness in VS 2005

    Hi Charles,

    Visual Studio correctly determines that this is an unhandled exception.
    You are trapping IndexOutOfRange Exception, but what occurs is a
    FormatException .

    I'm guessing that the line

    Console.WriteLi ne("nums[{0}] : {i}", i, nums[i]);

    should actually be

    Console.WriteLi ne("nums[{0}] : {1}", i, nums[i]);

    {1} instead of {i}


    On Mon, 04 Sep 2006 14:16:01 +0200, CharlesA
    <CharlesA@discu ssions.microsof t.comwrote:
    Hi folks,
    I'm working on code from Herb Schildt's book in which he says that an
    exception can be thrown by one method and caught by another
    >
    class ExcTest
    {
    public static void genException()
    {
    int[] nums = new int[4];
    Console.WriteLi ne("Before exception is generated");
    //generate an index out-of-bounds exception
    for (int i = 0; i < 10; i++)
    {
    nums[i] = i;
    Console.WriteLi ne("nums[{0}] : {i}", i, nums[i]);
    }
    >
    Console.WriteLi ne("this won't be displayed");
    }
    }
    >
    class ExcDemo2
    {
    public static void Main()
    {
    try
    {
    ExcTest.genExce ption();
    >
    }
    catch (IndexOutOfRang eException)
    {
    //catch the exception
    Console.WriteLi ne("Index out-of-bounds!");
    }
    Console.WriteLi ne("After catch statement.");
    }
    }
    >
    what's supposed to happen is that calling ExcTest.genExce ption() will
    generate the exception in genExcetpion (which has no exception handling)
    and
    so will therefore be handled in the caller Main() which does!
    >
    however all I get is Visual studio 2005 intruding into this scenario
    with an
    over-detailed exception dialog box that highlights the offending line in
    yellow and goes into debug mode.
    >
    is there a way I can get Main() to handle to the exception without having
    VStudio intrude, is there some setting somewhere?
    >
    Regards, and thanks in advance,
    CharlesA
    >


    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • CharlesA

      #3
      Re: Exception weirdness in VS 2005

      haha
      thanks a million Morten, I really really appreciate your post, because not
      only have I blundered with a typo and then blamed the wholly innocent
      VStudio, but I didn't pay any attention to the wording on the dialog , so
      sure was I that I had an IndexOutOfRange Exception, people, eh!!

      I've also gleaned from you're saying that if you don't handle the exception,
      the CLR will throw it and (if you're using VS) VStudio will have to
      intervene...thi s is what I excpeted, but I hadn't reckoned on my poor typing!


      thanks a million again!
      Regards,
      CharlesA

      Comment

      • Morten Wennevik

        #4
        Re: Exception weirdness in VS 2005

        Glad I could be of service :)

        You can handle different types of exceptions like this

        try
        {
        ExcTest.genExce ption();

        }
        catch (IndexOutOfRang eException)
        {
        //catch the exception
        Console.WriteLi ne("Index out-of-bounds!");
        }
        catch (FormatExceptio n)
        {
        //catch the exception
        Console.WriteLi ne("Wrong format!");
        }
        catch (Exception ex)
        {
        Console.WriteLi ne("Unknown error!\r\n" + ex.Message);
        }

        The order is important though, with the general exceptions below the
        specific ones.


        On Mon, 04 Sep 2006 14:50:01 +0200, CharlesA
        <CharlesA@discu ssions.microsof t.comwrote:
        haha
        thanks a million Morten, I really really appreciate your post, because
        not
        only have I blundered with a typo and then blamed the wholly innocent
        VStudio, but I didn't pay any attention to the wording on the dialog , so
        sure was I that I had an IndexOutOfRange Exception, people, eh!!
        >
        I've also gleaned from you're saying that if you don't handle the
        exception,
        the CLR will throw it and (if you're using VS) VStudio will have to
        intervene...thi s is what I excpeted, but I hadn't reckoned on my poor
        typing!
        >
        >
        thanks a million again!
        Regards,
        CharlesA


        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • CharlesA

          #5
          Re: Exception weirdness in VS 2005

          cheers again for your latest post
          forums are a wonderful thing!
          Regards,
          Charles

          Comment

          Working...