Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old June 12th, 2004, 12:01 PM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

 
Old June 12th, 2004, 05:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Could you post any code? Don't have the book.

Thanks,

Brian
 
Old June 12th, 2004, 09:57 PM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
         }


      }
   }
}

 
Old June 13th, 2004, 05:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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;
         }
    

     }
}
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
COM Exception muralidharan.d VS.NET 2002/2003 0 August 7th, 2007 02:49 PM
Exception Ochi C# 1 January 15th, 2006 11:38 PM
Problem in bean lookup, no exception avaniamin J2EE 0 November 11th, 2005 05:40 AM
Exception Occured (0x80020009) problem dominic_huang Classic ASP Databases 1 November 17th, 2004 01:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.