C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet458/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   454   455   456   457   458   459   460   461   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

NullReferenceException

:

// Use the NullReferenceException.



using System;

class X {

  int x;

  public X(int a) {

    x = a;

  }


  public int Add(X o) {

    return x + o.x;

  }

}

// Demonstrate NullReferenceException.



class NREDemo {

  static void Main() {

    X p = new X(10);

    X q = null; // q is explicitly assigned null

    int val;

Exception

Meaning

ArrayTypeMismatchException

Type of value being stored is incompatible with the type of 

the array.

DivideByZeroException

Division by zero attempted.

IndexOutOfRangeException

Array index is out of bounds.

InvalidCastException

A runtime cast is invalid.

OutOfMemor yException

Insufficient free memor y exists to continue program 

execution. For example, this exception will be thrown if 

there is not sufficient free memor y to create an object via 

new

.

Over flowException



An arithmetic over flow occurred.

NullReferenceException

An attempt was made to operate on a null reference—that 

is, a reference that does not refer to an object.

StackOver flowException

The stack was overrun.

T

ABLE


 13-1 

Commonly Used Exceptions Defi ned Within the 

System

 Namespace



www.freepdf-books.com


354

 

P a r t   I :  



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

    try {

      val = p.Add(q); // this will lead to an exception

    } catch (NullReferenceException) {

      Console.WriteLine("NullReferenceException!");

      Console.WriteLine("fixing...\n");

      // Now, fix it.

      q = new X(9);

      val = p.Add(q);

    }


    Console.WriteLine("val is {0}", val);

  }


}

The output from the program is shown here:

NullReferenceException!

fixing...

val is 19

The program creates a class called 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   454   455   456   457   458   459   460   461   ...   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