কম্পিউটার টিউটোরিয়াল

জাভাতে এক্সসেপশন রি-থ্রো (Re-throwing Exceptions) বলতে কী বোঝায়?

যখন একটি এক্সসেপশন ক্যাচ ব্লকে ধরা পড়ে, তখন আপনি throw কীওয়ার্ড ব্যবহার করে এটিকে রি-থ্রো করতে পারেন (যা এক্সসেপশন অবজেক্ট থ্রো করতে ব্যবহৃত হয়)।

রি-থ্রো করার সময় আপনি একই এক্সসেপশনটি যেনেরেই কোনো সমায়োজন ছাড়াই থ্রো করতে পারেন −

try { 
    int result = (arr[a])/(arr[b]); 
    System.out.println("Result of " + arr[a] + "/" + arr[b] + ": " + result); 
} catch(ArithmeticException e) { 
    throw e; 
}

অথবা, এটি একটি নতুন এক্সসেপশনের মধ্যে মোড়ে থ্রো করতে পারেন। যখন আপনি একটি ক্যাচড এক্সসেপশন অন্য একটি এক্সসেপশনের মধ্যে মোড়ে থ্রো করেন, তখন এটি এক্সসেপশন চেইনিং বা এক্সসেপশন র‍্যাপিং নামে পরিচিত, এটি করার মাধ্যমে আপনি আপনার এক্সসেপশন সমায়োজন করতে পারেন, একটি উচ্চ স্তরের এক্সসেপশন থ্রো করতে পারেন অ্যাবস্ট্রাকশন বজায় রাখते।

try { 
    int result = (arr[a])/(arr[b]); 
    System.out.println("Result of " + arr[a] + "/" + arr[b] + ": " + result); 
} catch(ArrayIndexOutOfBoundsException e) { 
    throw new IndexOutOfBoundsException(); 
}

উদাহরণ

নিচের জাভা উদাহরণে আমাদের demoMethod()-এ কোড ArrayIndexOutOfBoundsException এবং ArithmeticException থ্রো করতে পারে। আমরা এই দুটি এক্সসেপশন দুটি ভিন্ন ক্যাচ ব্লকে ক্যাচ করছি।

ক্যাচ ব্লকে, আমরা উভয় এক্সসেপশন রি-থ্রো করছি, একটি হায়ার এক্সসেপশনের মধ্যে মোড়ে এবং অন্যটি সরাসরি।

import java.util.Arrays; 
import java.util.Scanner; 

public class RethrowExample { 
    public void demoMethod() { 
        Scanner sc = new Scanner(System.in); 
        int[] arr = {10, 20, 30, 2, 0, 8}; 
        System.out.println("Array: " + Arrays.toString(arr)); 
        System.out.println("Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)"); 
        
        int a = sc.nextInt(); 
        int b = sc.nextInt(); 
        
        try { 
            int result = (arr[a])/(arr[b]); 
            System.out.println("Result of " + arr[a] + "/" + arr[b] + ": " + result); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
            throw new IndexOutOfBoundsException(); 
        } 
        catch(ArithmeticException e) { 
            throw e; 
        } 
    } 
    
    public static void main(String [] args) { 
        new RethrowExample().demoMethod(); 
    } 
}

আউটপুট১

Array: [10, 20, 30, 2, 0, 8] 
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 
0 4 
Exception in thread "main" java.lang.ArithmeticException: / by zero 
    at myPackage.RethrowExample.demoMethod(RethrowExample.java:16) 
    at myPackage.RethrowExample.main(RethrowExample.java:25)

আউটপুট২

Array: [10, 20, 30, 2, 0, 8] 
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 
124 5 
Exception in thread "main" java.lang.IndexOutOfBoundsException 
    at myPackage.RethrowExample.demoMethod(RethrowExample.java:17) 
    at myPackage.RethrowExample.main(RethrowExample.java:23)
  1. জাভাতে StringIndexOutOfBoundsException কী?

  2. জাভা প্রোগ্রামিং কী

  3. রুবি এক্সেপশন মাস্টার করুন: আত্মবিশ্বাসের সাথে এরর হ্যান্ডলিং

  4. জাভা এক্সেপশন হ্যান্ডলিং মাস্টার করুন: সম्पূর্ণ গাইড