Page 169 - CITS - CSA - TP (Volume 2) - Hindi
P. 169
कं ूटर सॉ वेयर ए ीके शन - CITS
अ ास 106: कॉमन इ े शन को संभाल (Handle common exceptions)
उ े
इस अ ास के अंत म आप यह कर सक गे
• िविभ इ े शन का उपयोग करके जावा ो ाम डेवलप करना।
आव कताएं (Requirements)
उपकरण/साम ी (Tools/Materials)
• िवंडोज OS वाला PC/लैपटॉप
• SDK सॉ वेयर
• टे एिडटर (िवजुअल ू िडयो/सबलाइम/नोटपैड)
ि या (Procedure)
1 NullPointerException:
• िववरण: यह तब होता है जब िकसी ऑ े के म बस (िविधयों या फ़ी ्स) तक प ँचने का यास िकया जाता है जो शू है।
• ह डिलंग ि कोण: अपने म बस तक प ँचने से पहले जाँच कर िक ा ऑ े शू है।
public class NullPointerExceptionExample {
public static void main(String[] args) {
try {
// Step 1: Declare a String variable and initialize it to null
String str = null;
// Step 2: Attempt to access the length() method on a null reference
int length = str.length(); // This will throw a NullPointerException
// Step 3: Display the length of the string (this won’t be reached due to the exception)
System.out.println(“Length of the string: “ + length);
} catch (NullPointerException e) {
// Step 4: Catch the NullPointerException and handle it
System.out.println(“Caught NullPointerException: “ + e.getMessage());
}
}
}
आउटपुट:
155

