C# 0 The Complete Reference


Exception  via  base



Download 4,07 Mb.
Pdf ko'rish
bet465/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   461   462   463   464   465   466   467   468   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Exception

 via 


base

. As 


explained, in cases in which your exception class does not add any functionality, you can 

simply let the 



Exception

 constructors handle the process. There is no requirement that your 

derived class add anything to what is inherited from 

Exception

.

Before moving on, you might want to experiment with this program a bit. For example, 



try commenting-out the override of 

ToString( )

 and observe the results. Also, try creating an 

exception using the default constructor, and observe what C# generates as its default message.

Exceptions'>Catching Derived Class Exceptions

You need to be careful how you order 



catch

 clauses when trying to catch exception types that 

involve base and derived classes, because a 

catch

 for a base class will also match any of its 

derived classes. For example, because the base class of all exceptions is 

Exception

, catching 



Exception

 catches all possible exceptions. Of course, using 



catch

 without an exception type 

provides a cleaner way to catch all exceptions, as described earlier. However, the issue of 

catching derived class exceptions is very important in other contexts, especially when you 

create exceptions of your own.

If you want to catch exceptions of both a base class type and a derived class type, put 

the derived class first in the 

catch

 sequence. This is necessary because a base class 



catch

 will 


also catch all derived classes. Fortunately, this rule is self-enforcing because putting the base 

class first causes a compile-time error.

The following program creates two exception classes called 

ExceptA

 and 


ExceptB

.

ExceptA

 is derived from 

Exception

.

ExceptB

 is derived from 

ExceptA

. The program then 

throws an exception of each type. For brevity, the custom exceptions supply only one 

constructor (which takes a string that describes the exception). But remember, in commercial 

code, your custom exception classes will normally provide all four of the constructors 

defined by 



Exception

.

// Derived exceptions must appear before base class exceptions.



using System;

// Create an exception.

class ExceptA : Exception {

  public ExceptA(string str) : base(str) { }

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   461   462   463   464   465   466   467   468   ...   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