473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Sep 4 '06 #1
4 1272
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[i]);

should actually be

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

{1} instead of {i}
On Mon, 04 Sep 2006 14:16:01 +0200, CharlesA
<Ch******@discussions.microsoft.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.WriteLine("Before exception is generated");
//generate an index out-of-bounds exception
for (int i = 0; i < 10; i++)
{
nums[i] = i;
Console.WriteLine("nums[{0}] : {i}", i, nums[i]);
}

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


--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 4 '06 #2
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
Sep 4 '06 #3
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.
On Mon, 04 Sep 2006 14:50:01 +0200, CharlesA
<Ch******@discussions.microsoft.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 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


--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 4 '06 #4
cheers again for your latest post
forums are a wonderful thing!
Regards,
Charles
Sep 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Straw | last post by:
I'm running into trouble when calling a 3rd party library (the Intel IPP library to do some fast math on a P4 processor). This is on debian linux 2.6.7 using python 2.3.4. I've been using Pyrex...
4
by: steve | last post by:
In a nutshell, my problem is that I am getting this runtime error, and I have no clue why. Please bear in mind its my first python program: "localvariable 'currentAbility' referenced before...
7
by: harborcoat-design | last post by:
Hello - I thought I was reasonably versed in JavaScript, buuuuut... When I do the following: x = parseFloat(326.655); y = parseFloat(100); z = x * y; alert(z);
2
by: Andy | last post by:
Hi all, I'm trying to debug a program I have. I have a windows service that has a timer which goes off to check for some files and process them. The event called to handle the Elaspsed event...
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
7
by: Chris | last post by:
Hello all... I have a program with the following structure (all classes mentioned are of my own creation, and none of the classes contain try or catch blocks): - main() consists of a large...
4
by: mmedved | last post by:
I ran into a problem using a custom ActiveX I wrote in VB 2005. It has a property that is "indexed" - that is, it has a parameter and needs to be addressed as (Index) - kinda like this: ...
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.