 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 12th, 2004, 12:01 PM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
a problem about the exception?
beginning c#
chapter 7
ch07Ex02
when function main() load the string "nested index" in array eTypes,there will emerge a exception "IndexOutOfRangeException",I think the exception will go to the "catch(System.IndexOutOfRangeException)" inside the function main,but it goes to "catch" inside the function of Throwexception,
WHY?
|
|

June 12th, 2004, 05:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hello,
Could you post any code? Don't have the book.
Thanks,
Brian
|
|

June 12th, 2004, 09:57 PM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I know now
Thank you all the same ,
the code as follows :
using System;
namespace Ch07Ex02
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static string[] eTypes = {"none", "simple", "index", "nested index"};
static void Main(string[] args)
{
foreach (string eType in eTypes)
{
try
{
Console.WriteLine("Main() try block reached."); // Line 18
Console.WriteLine("ThrowException(\"{0}\") called.", eType);
// Line 19
ThrowException(eType);
Console.WriteLine("Main() try block continues."); // Line 21
}
catch (System.IndexOutOfRangeException e) // Line 23
{
Console.WriteLine("Main() System.IndexOutOfRangeException catch"
+ " block reached. Message:\n\"{0}\"",
e.Message);
}
catch // Line 29,û°ãµÃÃì³££¡
{
Console.WriteLine("Main() general catch block reached.");
}
finally
{
Console.WriteLine("Main() finally block reached.");
}
Console.WriteLine();
}
}
static void ThrowException(string exceptionType)
{
// Line 43
Console.WriteLine("ThrowException(\"{0}\") reached.", exceptionType);
switch (exceptionType)
{
case "none" :
Console.WriteLine("Not throwing an exception.");
break; // Line 48
case "simple" :
Console.WriteLine("Throwing System.Exception.");
throw (new System.Exception()); // Line 51
break;
case "index" :
Console.WriteLine("Throwing System.IndexOutOfRangeException.");
eTypes[4] = "error"; // Line 55
break;
case "nested index" :
try // Line 58
{
Console.WriteLine("ThrowException(\"nested index\") " +
"try block reached.");
Console.WriteLine("ThrowException(\"index\") called.");
ThrowException("index"); // Line 63
}
catch // Line 65
{
Console.WriteLine("ThrowException(\"nested index\") general"
+ " catch block reached.");
}
finally
{
Console.WriteLine("ThrowException(\"nested index\") finally"
+ " block reached.");
}
break;
}
}
}
}
|
|

June 13th, 2004, 05:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
So what was the problem, or no problem at all?
Quote:
quote:Originally posted by watson
I know now
Thank you all the same ,
the code as follows :
using System;
namespace Ch07Ex02
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static string[] eTypes = {"none", "simple", "index", "nested index"};
static void Main(string[] args)
{
foreach (string eType in eTypes)
{
try
{
Console.WriteLine("Main() try block reached."); // Line 18
Console.WriteLine("ThrowException(\"{0}\") called.", eType);
// Line 19
ThrowException(eType);
Console.WriteLine("Main() try block continues."); // Line 21
}
catch (System.IndexOutOfRangeException e) // Line 23
{
Console.WriteLine("Main() System.IndexOutOfRangeException catch"
+ " block reached. Message:\n\"{0}\"",
e.Message);
}
catch // Line 29,û°ãµÃÃì³££¡
{
Console.WriteLine("Main() general catch block reached.");
}
finally
{
Console.WriteLine("Main() finally block reached.");
}
Console.WriteLine();
}
}
static void ThrowException(string exceptionType)
{
// Line 43
Console.WriteLine("ThrowException(\"{0}\") reached.", exceptionType);
switch (exceptionType)
{
case "none" :
Console.WriteLine("Not throwing an exception.");
break; // Line 48
case "simple" :
Console.WriteLine("Throwing System.Exception.");
throw (new System.Exception()); // Line 51
break;
case "index" :
Console.WriteLine("Throwing System.IndexOutOfRangeException.");
eTypes[4] = "error"; // Line 55
break;
case "nested index" :
try // Line 58
{
Console.WriteLine("ThrowException(\"nested index\") " +
"try block reached.");
Console.WriteLine("ThrowException(\"index\") called.");
ThrowException("index"); // Line 63
}
catch // Line 65
{
Console.WriteLine("ThrowException(\"nested index\") general"
+ " catch block reached.");
}
finally
{
Console.WriteLine("ThrowException(\"nested index\") finally"
+ " block reached.");
}
break;
}
}
}
}
|
|
|
 |