Saturday 15 January 2022

Question 9. What is exception handling? What are the types of exceptions? Write a code to handle array out of bound exceptions. अपवाद हैंडलिंग क्या है? अपवाद कितने प्रकार के होते हैं? Array out of bound अपवाद को संभालने के लिए एक कोड लिखें।


Exception handling mechanism describes the behavior of program when exception occurs. Exception handling is a very powerful technique by which runtime errors are handled. There are two ways of handling exceptions: programmer defined exception handling and default exception handling.

अपवाद प्रबंधन तंत्र, अपवाद होने पर Program(Software Code) के व्यवहार का वर्णन करता है। Exception Handling एक बहुत ही शक्तिशाली तकनीक है जिसके द्वारा runtime errors को handle किया जाता है। जिससे program का normal flow बना रहता है। अपवादों को संभालने के दो तरीके हैं: प्रोग्रामर परिभाषित अपवाद हैंडलिंग और डिफ़ॉल्ट अपवाद हैंडलिंग।


Types of Exception:-

There can be two types of built-in exception classes namely, checked exceptions and unchecked exceptions. Programmer needs to explicitly mention about handling of checked exceptions in the program where as not necessarily about unchecked exceptions.

दो प्रकार के अंतर्निर्मित अपवाद Class हो सकते हैं,  अर्थात् चेक किए गए अपवाद और अनियंत्रित अपवाद। प्रोग्रामर को प्रोग्राम में चेक किए गए अपवादों को संभालने के बारे में स्पष्ट रूप से उल्लेख करने की आवश्यकता होती है। जबकि अनियंत्रित अपवादों के लिए यह आवश्‍यक नही है।

 

Checked Exception

Checked exceptions are either handled by the method that generate them using a pair of keyword try and catch or it may be explicitly declared using keyword throws. Hence, compiler checks these exceptions. Below table shows some exception conditions and their corresponding checked exception classes.

Checked Exceptions को या तो ट्राई व कैच कीवर्ड का उपयोग करके नियंत्रित किया जा सकता है या फिर स्‍पष्‍ट रूप से थ्रो कीवर्ड का उपयोग करके नियंत्रित किया जा सकता है। इसलिए,  कंपाइलर इन अपवादों की जाँच करता है। नीचे दी गई तालिका कुछ अपवाद स्थितियों और उनके संबंधित Checked Exception क्‍लास को दिखाती है।

 

S.No.

Condition

Checked Exception

1.

Class Not Found

ClassNotFoundException

2.

Clone not supported

CloneNotSupportedException

3.

Illegal access to a class

IllegalAccessException

4.

Instantiation not allowed

InstantiationException

5.

Interrupted by thread

InterruptedException

6.

Field not found

NoSuchFieldException

7.

Method does not exist

NoSuchMethodException

 

UNCHECKED Exception

Unchecked exceptions occur at run time and are not checked by compiler. Programmer is not compelled to write exception handler for them rather it is optional. Below table shows some common exception conditions and their corresponding unchecked exception classes.

अनियंत्रित अपवाद रन टाइम पर होते हैं और कंपाइलर द्वारा चेक नहीं किए जाते हैं। प्रोग्रामर उनके लिए अपवाद हैंडलर लिखने के लिए बाध्य नहीं है बल्कि यह वैकल्पिक है। नीचे दी गई तालिका कुछ सामान्य अपवाद स्थितियों और उनके संबंधित Unchecked Exception क्‍लास को दिखाती है।

S.No.

Conditions

Unchecked Exception

1

Divide by zero

ArithmeticException

2

Array index out of bounds

Array Index Out Of Bounds Exception

3

Not in a number format

NumberFormatException

4

Improper type array storage

ArrayStoreException

5

Invalid cast

ClassCastException

6

Illegal argument type

IllegalArgumentException

7

Negative array size

NegativeArraySizeException

8

Invalid use of null pointer

NullPointerException

 

 

Code to handle array out of bound exceptions

 

public class Main {
   public static void main (String args[]) {
      int array[] = {20,20,40};
      int num1 = 15, num2 = 10;
      int result = 10;
      try {
         result = num1/num2;
         System.out.println("The result is" +result);
         for(int i = 5; i >= 0; i--) {
            System.out.println("The value of array is" +array[i]);
         }
      } catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("Array is out of Bounds"+e);
      } catch (ArithmeticException e) {
         System.out.println ("Can't divide by Zero"+e);
      }
   }
}
 
Output
 
The result is1
Array is out of Boundsjava.lang.ArrayIndexOutOfBoundsException : 5

 


 


No comments:

Post a Comment