Exception weirdness in VS 2005

G

Guest

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.WriteLine("Before exception is generated");
//generate an index out-of-bounds exception
for (int i = 0; i < 10; i++)
{
nums = i;
Console.WriteLine("nums[{0}] : {i}", i, nums);
}

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

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

}
catch (IndexOutOfRangeException)
{
//catch the exception
Console.WriteLine("Index out-of-bounds!");
}
Console.WriteLine("After catch statement.");
}
}

what's supposed to happen is that calling ExcTest.genException() 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
 
M

Morten Wennevik

Hi Charles,

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

I'm guessing that the line

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

should actually be

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

{1} instead of {i}


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.WriteLine("Before exception is generated");
//generate an index out-of-bounds exception
for (int i = 0; i < 10; i++)
{
nums = i;
Console.WriteLine("nums[{0}] : {i}", i, nums);
}

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

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

}
catch (IndexOutOfRangeException)
{
//catch the exception
Console.WriteLine("Index out-of-bounds!");
}
Console.WriteLine("After catch statement.");
}
}

what's supposed to happen is that calling ExcTest.genException() 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
 
G

Guest

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 IndexOutOfRangeException, 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...this is what I excpeted, but I hadn't reckoned on my poor typing!


thanks a million again!
Regards,
CharlesA
 
M

Morten Wennevik

Glad I could be of service :)

You can handle different types of exceptions like this

try
{
ExcTest.genException();

}
catch (IndexOutOfRangeException)
{
//catch the exception
Console.WriteLine("Index out-of-bounds!");
}
catch (FormatException)
{
//catch the exception
Console.WriteLine("Wrong format!");
}
catch (Exception ex)
{
Console.WriteLine("Unknown error!\r\n" + ex.Message);
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top