C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet439/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   435   436   437   438   439   440   441   442   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

nums

 is an 


int

 array of four elements. However, the 



for

 loop tries to index 



nums

from 0 to 9, which causes an 



IndexOutOfRangeException

 to occur when an index value of 

4 is tried.

Although quite short, the preceding program illustrates several key points about exception 

handling. First, the code that you want to monitor for errors is contained within a 

try

 block. 


Second, when an exception occurs (in this case, because of the attempt to index 

nums

beyond its bounds inside the 



for

 loop), the exception is thrown out of the 



try

 block and 

caught by the 

catch

. At this point, control passes to the 



catch

 block, and the 



try

 block is 

terminated. That is, 

catch

 is 


not

 called. Rather, program execution is transferred to it. Thus, 

the

WriteLine( )

 statement following the out-of-bounds index will never execute. After the 



catch

 block executes, program control continues with the statements following the 



catch

.

Thus, it is the job of your exception handler to remedy the problem that caused the 



exception so program execution can continue normally.

www.freepdf-books.com




340

 

P a r t   I :  



T h e   C #   L a n g u a g e

Notice that no exception variable is specified in the 



catch

 clause. Instead, only the type 

of the exception (

IndexOutOfRangeException

 in this case) is required. As mentioned, an 

exception variable is needed only when access to the exception object is required. In some 

cases, the value of the exception object can be used by the exception handler to obtain 

additional information about the error, but in many cases, it is sufficient to simply know 

that an exception occurred. Thus, it is not unusual for the 



catch

 variable to be absent in the 

exception handler, as is the case in the preceding program.

As explained, if no exception is thrown by a 



try

 block, no 



catch

 will be executed and 

program control resumes after the 

catch

. To confirm this, in the preceding program, change 

the

for

 loop from

for(int i=0; i < 10; i++) {

to

for(int i=0; i < nums.Length; i++) {



Now, the loop does not overrun 

nums

’ boundary. Thus, no exception is generated, and the 



catch

 block is not executed.



A Second Exception Example

It is important to understand that all code executed within a 



try

 block is monitored for 

exceptions. This includes exceptions that might be generated by a method called from 

within the 



try

 block. An exception thrown by a method called from within a 



try

 block 


can be caught by that 

try

 block, assuming, of course, that the method itself did not catch 

the exception.

For example, consider the following program. 



Main( )

 establishes a 



try

 block from which 

the method 

GenException( )

 is called. Inside 



GenException( )

, an 


IndexOutOfRangeException

is generated. This exception is not caught by 



GenException( )

. However, since 



GenException( ) 

was called from within a 



try

 block in 



Main( )

, the exception is caught by the 



catch

 statement 

associated with that 

try

.

/* An exception can be generated by one



   method and caught by another. */

using System;

class ExcTest {

  // Generate an exception.

  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}]: {1}", i, nums[i]);

    }


www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   435   436   437   438   439   440   441   442   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish