Java Technologies Generics এবং Exception Handling গাইড ও নোট

331

জাভার Generics এবং Exception Handling একত্রে ব্যবহারে কিছু নিয়ম এবং সীমাবদ্ধতা রয়েছে। জেনেরিক্স টাইপ নিরাপত্তা এবং কোড পুনঃব্যবহারযোগ্যতা বাড়ায়, তবে একে Exception এর সাথে ব্যবহার করার সময় সতর্ক হতে হয়, বিশেষত Type Erasure এর প্রভাবের কারণে।


Generics এবং Exception Handling: গুরুত্বপূর্ণ পয়েন্টসমূহ

  1. জেনেরিক ক্লাস বা মেথড থেকে Checked Exception থ্রো করা যাবে।
  2. Generics এর সাথে Exception Subtyping এবং Catching সঠিকভাবে পরিচালনা করতে হবে।
  3. জেনেরিক টাইপ প্যারামিটার Exception এর টাইপ হিসেবে ব্যবহার করা যায় না।
  4. Type Erasure এর কারণে জেনেরিক্স এর কিছু সীমাবদ্ধতা রয়েছে।

Generics এর সাথে Checked এবং Unchecked Exception

Checked Exception এবং Generics

  • Checked Exception কে Compile-Time এ হ্যান্ডেল করতে হয়।
  • উদাহরণ:

    public class GenericException<T extends Exception> {
        private T exception;
    
        public GenericException(T exception) {
            this.exception = exception;
        }
    
        public void throwException() throws T {
            throw exception;
        }
    
        public static void main(String[] args) {
            try {
                GenericException<Exception> ge = new GenericException<>(new Exception("Generic Exception!"));
                ge.throwException();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

Unchecked Exception এবং Generics

  • Unchecked Exception (RuntimeException) এর জন্য কম্পাইলারের কোনো বাধ্যবাধকতা নেই।
  • উদাহরণ:

    public class GenericRuntimeException<T extends RuntimeException> {
        private T exception;
    
        public GenericRuntimeException(T exception) {
            this.exception = exception;
        }
    
        public void throwRuntimeException() {
            throw exception;
        }
    
        public static void main(String[] args) {
            GenericRuntimeException<RuntimeException> gre = 
                new GenericRuntimeException<>(new IllegalArgumentException("Runtime Exception!"));
            gre.throwRuntimeException();
        }
    }
    

Generics এর সাথে Exception Handling এর সীমাবদ্ধতা

1. Generics এর মাধ্যমে Throwable Type ব্যবহার

  • Throwable টাইপ সরাসরি জেনেরিক প্যারামিটার হিসেবে ব্যবহার করা যায় না।
  • উদাহরণ:

    // ভুল উদাহরণ:
    public class GenericClass<T extends Throwable> {
        public void throwException(T exception) throws T {
            throw exception; // Compilation error
        }
    }
    
  • কারণ:
    • জেনেরিক্স Type Erasure এর ফলে Runtime এ টাইপ ইনফরমেশন হারিয়ে ফেলে।
    • Throwable এর টাইপ নির্ধারণ করতে কম্পাইলার সমস্যায় পড়ে।

2. Generic Exception ক্যাচ করা নিষিদ্ধ

  • Generic Exception ক্যাচ করা সম্ভব নয়।
  • উদাহরণ:

    try {
        // Code that may throw an exception
    } catch (T e) { // Compilation error
        System.out.println(e.getMessage());
    }
    
  • কারণ:
    • জাভার টাইম-রান এ Type Erasure এর কারণে জেনেরিক টাইপের সঠিক Exception নির্ধারণ করা যায় না।

Generics এর সাথে Exception Handling এর ভালো উদাহরণ

1. Generic Method এবং Checked Exception

  • উদাহরণ:

    public class GenericCheckedException {
        public static <T extends Exception> void throwAsException(Exception e) throws T {
            throw (T) e;
        }
    
        public static void main(String[] args) {
            try {
                throwAsException(new Exception("Checked Exception using Generics"));
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

2. Custom Generic Exception

  • উদাহরণ:

    public class GenericCustomException<T> extends Exception {
        private T details;
    
        public GenericCustomException(String message, T details) {
            super(message);
            this.details = details;
        }
    
        public T getDetails() {
            return details;
        }
    
        public static void main(String[] args) {
            try {
                throw new GenericCustomException<>("Custom Exception Occurred", 404);
            } catch (GenericCustomException<Integer> e) {
                System.out.println(e.getMessage() + ", Details: " + e.getDetails());
            }
        }
    }
    

Generics এবং Exception Handling এর Best Practices

  1. Checked Exception এর সাথে জেনেরিক্স ব্যবহার করলে সাবধানে টাইপ ডিক্লেয়ার করুন।
    • উদাহরণ:

      public class GenericHandler<T extends Exception> {
          public void handleException(T exception) throws T {
              throw exception;
          }
      }
      
  2. Throwable বা Exception এর Subclass কে জেনেরিক টাইপ হিসেবে ব্যবহারের চেষ্টা করুন।
    • যেমন:

      public class CustomHandler<T extends Throwable> {
          public void process(T throwable) throws T {
              throw throwable;
          }
      }
      
  3. Generic Method ব্যবহার করুন যেখানে Exception টাইপ ডাইনামিক হতে পারে।
    • উদাহরণ:

      public static <T extends Throwable> void rethrow(Throwable throwable) throws T {
          throw (T) throwable;
      }
      
  4. Wildcards (?) ব্যবহার করুন যখন Exception টাইপ ফিক্সড নয়।
  5. জেনেরিক Exception ক্লাস তৈরির সময় যথাযথ কাস্টমাইজেশন করুন।

জেনেরিক্স এবং Exception Handling একত্রে ব্যবহার:

  1. টাইপ সেফটি নিশ্চিত করে।
  2. কোড রিইউজেবিলিটি বাড়ায়।
  3. Runtime Exception এর ঝুঁকি কমায়।

তবে Type Erasure এবং জাভার Exception Handling Mechanism এর কারণে কিছু সীমাবদ্ধতা রয়েছে, যা সচেতনভাবে এড়িয়ে যাওয়া উচিত। সঠিকভাবে ব্যবহারে জেনেরিক্স এবং Exception Handling এর মিশ্রণ অ্যাপ্লিকেশন ডেভেলপমেন্টে একটি শক্তিশালী সমাধান দিতে পারে।

Content added By

Exception Handling এর ক্ষেত্রে Generics এর ভূমিকা

347

জাভায় Generics সাধারণত টাইপ সেফটি এবং কোডের পুনরায় ব্যবহারযোগ্যতা নিশ্চিত করার জন্য ব্যবহৃত হয়। Exception Handling এর ক্ষেত্রে Generics বিশেষ ভূমিকা পালন করে। Generics এর মাধ্যমে আমরা কাস্টম এক্সসেপশন হ্যান্ডলিং সহজ করতে পারি এবং টাইপ নির্ভরশীল (type-dependent) এক্সসেপশন হ্যান্ডলিংয়ে টাইপ সেফ সমাধান প্রদান করতে পারি।


Exception Handling এ Generics এর ব্যবহার

1. Custom Generic Exception Class

Generic Exception ক্লাস তৈরি করে আমরা বিভিন্ন টাইপের এক্সসেপশন পরিচালনা করতে পারি।

উদাহরণ:

public class GenericException<T> extends Exception {
    private T details;

    public GenericException(String message, T details) {
        super(message);
        this.details = details;
    }

    public T getDetails() {
        return details;
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new GenericException<>("An error occurred", 404);
        } catch (GenericException<Integer> e) {
            System.out.println(e.getMessage() + " - Error Code: " + e.getDetails());
        }

        try {
            throw new GenericException<>("Another error occurred", "Invalid Data");
        } catch (GenericException<String> e) {
            System.out.println(e.getMessage() + " - Details: " + e.getDetails());
        }
    }
}

ব্যাখ্যা:

  • এই উদাহরণে, একটি কাস্টম জেনেরিক এক্সসেপশন ক্লাস তৈরি করা হয়েছে যা বিভিন্ন ডেটা টাইপের এক্সসেপশন ডিটেইলস হ্যান্ডেল করতে পারে।
  • এতে টাইপ সেফটি নিশ্চিত হয় এবং এক্সসেপশন মেসেজ আরও কাস্টমাইজযোগ্য হয়।

2. Generic Utility for Exception Handling

Generic মেথড ব্যবহার করে বিভিন্ন ধরনের এক্সসেপশন হ্যান্ডল করার জন্য একটি ইউটিলিটি তৈরি করা যায়।

উদাহরণ:

public class ExceptionHandler {
    public static <T extends Exception> void handleException(T exception) {
        System.out.println("Handling exception of type: " + exception.getClass().getName());
        System.out.println("Message: " + exception.getMessage());
    }

    public static void main(String[] args) {
        try {
            throw new IllegalArgumentException("Invalid argument provided!");
        } catch (IllegalArgumentException e) {
            ExceptionHandler.handleException(e);
        }

        try {
            throw new NullPointerException("Null value encountered!");
        } catch (NullPointerException e) {
            ExceptionHandler.handleException(e);
        }
    }
}

ব্যাখ্যা:

  • Generic মেথড ব্যবহার করে বিভিন্ন ধরনের এক্সসেপশন হ্যান্ডল করা হয়েছে।
  • এখানে টাইপ সেফটি নিশ্চিত হয়েছে এবং একই মেথড পুনরায় ব্যবহারযোগ্য হয়েছে।

3. Multiple Exceptions with Generics

Generics ব্যবহার করে একাধিক টাইপের এক্সসেপশন হ্যান্ডল করা যায়।

উদাহরণ:

public class MultiExceptionHandler<T extends Exception> {
    public void handle(T exception) {
        System.out.println("Caught Exception: " + exception.getClass().getName());
        System.out.println("Message: " + exception.getMessage());
    }

    public static void main(String[] args) {
        MultiExceptionHandler<IllegalArgumentException> handler1 = new MultiExceptionHandler<>();
        handler1.handle(new IllegalArgumentException("Invalid argument!"));

        MultiExceptionHandler<NullPointerException> handler2 = new MultiExceptionHandler<>();
        handler2.handle(new NullPointerException("Null value found!"));
    }
}

ব্যাখ্যা:

  • Generics এর মাধ্যমে একই হ্যান্ডলার একাধিক ধরনের এক্সসেপশন পরিচালনা করতে সক্ষম হয়েছে।

Generics এবং Checked Exceptions

Generics এর কিছু সীমাবদ্ধতা Checked Exceptions এর ক্ষেত্রে রয়েছে:

  1. Generics এর Type Erasure এর কারণে Checked Exceptions সরাসরি টাইপ প্যারামিটারে ব্যবহার করা যায় না।
  2. কম্পাইল টাইমে Checked Exceptions এর জন্য জেনেরিক মেথড তৈরি করাও সম্ভব নয়।

Workaround:

Unchecked Exceptions (যেমন RuntimeException) এর ক্ষেত্রে Generics বেশ কার্যকর। Checked Exceptions পরিচালনার জন্য বিকল্প পদ্ধতি ব্যবহার করতে হয়।


4. Generic Wrapper for Exceptions

Generics ব্যবহার করে এক্সসেপশন র‍্যাপার তৈরি করা যায় যা এক্সসেপশন ডেটা সুরক্ষিত রাখে।

উদাহরণ:

public class ExceptionWrapper<T> {
    private T exceptionData;

    public ExceptionWrapper(T exceptionData) {
        this.exceptionData = exceptionData;
    }

    public T getExceptionData() {
        return exceptionData;
    }

    public static void main(String[] args) {
        ExceptionWrapper<String> stringWrapper = new ExceptionWrapper<>("File not found!");
        System.out.println("Exception Details: " + stringWrapper.getExceptionData());

        ExceptionWrapper<Integer> intWrapper = new ExceptionWrapper<>(404);
        System.out.println("Error Code: " + intWrapper.getExceptionData());
    }
}

ব্যাখ্যা:

  • এক্সসেপশন সংক্রান্ত ডেটা একটি জেনেরিক র‍্যাপারের মাধ্যমে হ্যান্ডেল করা হয়েছে।
  • এটি টাইপ সেফ এবং আরও সুনির্দিষ্ট এক্সসেপশন ডিটেইলস নিশ্চিত করে।

Generics এর মাধ্যমে Exception Handling এর ক্ষেত্রে:

  1. টাইপ সেফটি নিশ্চিত হয়।
  2. কাস্টম এক্সসেপশন ক্লাস তৈরি করা সহজ হয়।
  3. একাধিক টাইপের এক্সসেপশন সহজেই পরিচালনা করা যায়।
  4. এক্সসেপশন সংক্রান্ত ডেটা আরও সুনির্দিষ্ট ও পুনরায় ব্যবহারযোগ্য হয়।

যদিও Generics Checked Exceptions এর ক্ষেত্রে সরাসরি কার্যকর নয়, Unchecked Exceptions এবং কাস্টম হ্যান্ডলিংয়ে এটি শক্তিশালী সমাধান প্রদান করে।

Content added By

Checked এবং Unchecked Exceptions এর সাথে Generics

306

জাভার জেনেরিক্স ব্যবহারের সময় Checked এবং Unchecked Exceptions নিয়ে কিছু চ্যালেঞ্জ এবং সীমাবদ্ধতা রয়েছে। এই চ্যালেঞ্জগুলো কীভাবে সমাধান করা যায় এবং Generics কীভাবে Exception Handling এর ক্ষেত্রে কার্যকর হয়, তা আমরা এই আলোচনায় তুলে ধরব।


Exception: Checked এবং Unchecked ব্যাখ্যা

  1. Checked Exceptions:
    • কম্পাইল টাইমে চেক করা হয়।
    • উদাহরণ: IOException, SQLException
    • মেথডের সিগনেচারে throws দিয়ে ডিক্লেয়ার করা বাধ্যতামূলক।
  2. Unchecked Exceptions:
    • রানটাইমে ঘটে।
    • উদাহরণ: NullPointerException, ArithmeticException
    • মেথডের সিগনেচারে ডিক্লেয়ার করা প্রয়োজন নেই।

Generics এবং Checked Exceptions এর চ্যালেঞ্জ

Generics এর টাইপ ইনফরমেশন Type Erasure এর মাধ্যমে কম্পাইল টাইমে মুছে যায়। ফলে Checked Exceptions এর ব্যবহার কিছু সীমাবদ্ধতার মুখোমুখি হয়। উদাহরণস্বরূপ, Generics এর সাথে Checked Exceptions সরাসরি টাইপ প্যারামিটারে ডিক্লেয়ার করা যায় না।

// এটি কম্পাইল হবে না
public class GenericClass<T extends Exception> {
    public void throwException(T exception) throws T {
        throw exception; // Compile Time Error
    }
}

Generics এবং Exception Handling এর উদাহরণ

1. Unchecked Exceptions এর সাথে Generics

Generics ব্যবহার করে টাইপ-সেফ ডেটা হ্যান্ডলিং করা যায়, যা রানটাইম ত্রুটি কমাতে সাহায্য করে।

public class GenericWrapper<T> {
    private T value;

    public GenericWrapper(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void validate() {
        if (value == null) {
            throw new NullPointerException("Value is null");
        }
    }
}

// Example Usage
public class Main {
    public static void main(String[] args) {
        GenericWrapper<String> wrapper = new GenericWrapper<>("Hello");
        wrapper.validate();
        System.out.println(wrapper.getValue()); // Output: Hello
    }
}

2. Checked Exceptions এর সাথে Workaround

Generics এবং Checked Exceptions এর সীমাবদ্ধতা এড়ানোর জন্য একটি Helper Method ব্যবহার করা যেতে পারে।

public class ExceptionUtil {
    @SuppressWarnings("unchecked")
    public static <T extends Throwable> void throwAs(Throwable e) throws T {
        throw (T) e;
    }
}

// Example Usage
public class Main {
    public static void main(String[] args) {
        try {
            ExceptionUtil.throwAs(new Exception("Checked Exception Example"));
        } catch (Exception e) {
            System.out.println(e.getMessage()); // Output: Checked Exception Example
        }
    }
}

3. Generic Method এবং Checked Exceptions

Generic Method ব্যবহার করে Checked Exceptions হ্যান্ডল করা যেতে পারে।

public class GenericExceptionExample {
    public static <T extends Exception> void throwCheckedException(T exception) throws T {
        throw exception;
    }
}

// Example Usage
public class Main {
    public static void main(String[] args) {
        try {
            GenericExceptionExample.throwCheckedException(new Exception("Checked Exception with Generics"));
        } catch (Exception e) {
            System.out.println(e.getMessage()); // Output: Checked Exception with Generics
        }
    }
}

4. Generics এবং Custom Exceptions

// Custom Checked Exception
class MyCheckedException extends Exception {
    public MyCheckedException(String message) {
        super(message);
    }
}

// Generic Class with Custom Exception
public class GenericProcessor<T> {
    public void process(T data) throws MyCheckedException {
        if (data == null) {
            throw new MyCheckedException("Data is null");
        }
        System.out.println("Processing: " + data);
    }
}

// Example Usage
public class Main {
    public static void main(String[] args) {
        GenericProcessor<String> processor = new GenericProcessor<>();
        try {
            processor.process("Generics with Exceptions"); // Output: Processing: Generics with Exceptions
            processor.process(null); // Throws MyCheckedException
        } catch (MyCheckedException e) {
            System.out.println(e.getMessage()); // Output: Data is null
        }
    }
}

Generics এবং Exception Handling এর সুবিধা

  1. Type-Safe Handling: Checked এবং Unchecked Exceptions এর ক্ষেত্রে টাইপ-সেফটি নিশ্চিত করা যায়।
  2. Reusable Code: Generics ব্যবহার করে একই Exception Handling কোড পুনঃব্যবহার করা যায়।
  3. Runtime Errors Minimized: কম্পাইল টাইমে Checked Exceptions চেক করা হয়, যা রানটাইম ত্রুটি কমায়।
  4. Flexible Design: Custom Exceptions এবং Generics ব্যবহার করে জটিল Exception Handling সহজ করা যায়।

Generics এর মাধ্যমে Exception Propagation

Generics ব্যবহার করে Exceptions এর Propagation টাইপ-সেফ এবং সহজ করা সম্ভব। নিচের উদাহরণটি এই ধারণাকে তুলে ধরে:

public class ExceptionPropagator<T extends Exception> {
    public void propagateException(T exception) throws T {
        throw exception;
    }
}

// Example Usage
public class Main {
    public static void main(String[] args) {
        ExceptionPropagator<IllegalArgumentException> propagator = new ExceptionPropagator<>();
        try {
            propagator.propagateException(new IllegalArgumentException("Illegal Argument!"));
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage()); // Output: Illegal Argument!
        }
    }
}

  1. Generics এবং Exception Handling একত্রে ব্যবহারে Checked এবং Unchecked Exceptions টাইপ-সেফ এবং পুনঃব্যবহারযোগ্যভাবে পরিচালনা করা যায়।
  2. Generics এর সীমাবদ্ধতা (যেমন Type Erasure) থাকার পরেও Helper Method এবং Custom Design ব্যবহার করে কার্যকর সমাধান সম্ভব।
  3. Checked Exceptions এর ক্ষেত্রে Generics ব্যবহার ডিজাইনের সময় আরও বেশি নিরাপত্তা এবং ফ্লেক্সিবিলিটি প্রদান করে।
Content added By

Generic Exception Class তৈরির কৌশল

274

জাভাতে Generic Exception Class তৈরি করে ডাইনামিক এবং পুনঃব্যবহারযোগ্য এক্সেপশন মেকানিজম তৈরি করা যায়। সাধারণত, এটি ব্যবহার করা হয় এমন অবস্থায় যেখানে এক্সেপশন টাইপের সাথে কিছু অতিরিক্ত টাইপ-ডেটা যুক্ত করতে হয়। Generic Exception Class টাইপ-সেইফটি নিশ্চিত করে এবং কোডের পুনঃব্যবহারযোগ্যতা বাড়ায়।


Generic Exception Class তৈরির উপকারিতা

  1. টাইপ-সেইফ এক্সেপশন হ্যান্ডলিং: কম্পাইল টাইমে টাইপ যাচাই করা যায়।
  2. ডাইনামিক ডেটা যোগ করা: এক্সেপশন ক্লাসে অতিরিক্ত টাইপযুক্ত ডেটা যোগ করা যায়।
  3. Reusable Design: একবার তৈরি করলে এটি বিভিন্ন প্রজেক্টে পুনঃব্যবহার করা যায়।
  4. কোড সহজ ও পরিষ্কার করা: কমন এক্সেপশন মেকানিজম তৈরি করে কোড কমপ্লেক্সিটি কমানো যায়।

Generic Exception Class এর উদাহরণ

১. একটি সাধারণ Generic Exception Class

public class GenericException<T> extends Exception {
    private T additionalInfo;

    public GenericException(String message, T additionalInfo) {
        super(message);
        this.additionalInfo = additionalInfo;
    }

    public T getAdditionalInfo() {
        return additionalInfo;
    }
}

ব্যবহার:

public class Main {
    public static void main(String[] args) {
        try {
            throw new GenericException<>("Custom Exception", 123);
        } catch (GenericException<Integer> e) {
            System.out.println("Exception Message: " + e.getMessage());
            System.out.println("Additional Info: " + e.getAdditionalInfo());
        }
    }
}

আউটপুট:

Exception Message: Custom Exception
Additional Info: 123

২. টাইপ অনুযায়ী ভিন্ন ডেটা হ্যান্ডলিং

public class TypedGenericException<T> extends Exception {
    private T errorDetail;

    public TypedGenericException(String message, T errorDetail) {
        super(message);
        this.errorDetail = errorDetail;
    }

    public T getErrorDetail() {
        return errorDetail;
    }
}

ব্যবহার:

public class Main {
    public static void main(String[] args) {
        try {
            processString("Invalid Data");
        } catch (TypedGenericException<String> e) {
            System.out.println("Error Message: " + e.getMessage());
            System.out.println("Error Detail: " + e.getErrorDetail());
        }
    }

    public static void processString(String input) throws TypedGenericException<String> {
        if ("Invalid Data".equals(input)) {
            throw new TypedGenericException<>("Invalid String Data Provided", input);
        }
    }
}

আউটপুট:

Error Message: Invalid String Data Provided
Error Detail: Invalid Data

৩. Complex Generic Exception Class

public class DetailedException<T, U> extends Exception {
    private T errorCode;
    private U errorDetails;

    public DetailedException(String message, T errorCode, U errorDetails) {
        super(message);
        this.errorCode = errorCode;
        this.errorDetails = errorDetails;
    }

    public T getErrorCode() {
        return errorCode;
    }

    public U getErrorDetails() {
        return errorDetails;
    }
}

ব্যবহার:

public class Main {
    public static void main(String[] args) {
        try {
            validateData(404, "Data not found");
        } catch (DetailedException<Integer, String> e) {
            System.out.println("Exception Message: " + e.getMessage());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Details: " + e.getErrorDetails());
        }
    }

    public static void validateData(int code, String details) throws DetailedException<Integer, String> {
        if (code == 404) {
            throw new DetailedException<>("Validation Failed", code, details);
        }
    }
}

আউটপুট:

Exception Message: Validation Failed
Error Code: 404
Error Details: Data not found

Generics Exception Class এর সীমাবদ্ধতা

  1. Checked Exception: Generics ব্যবহার করে শুধুমাত্র Checked Exception তৈরি করা সম্ভব।
  2. Type Erasure: Generics এর টাইপ ইনফরমেশন Type Erasure এর মাধ্যমে কম্পাইল টাইমে মুছে ফেলা হয়, যা কিছু ক্ষেত্রে সীমাবদ্ধতা আনতে পারে।
  3. Performance Impact: যদিও টাইপ ইনফরমেশন মুছে ফেলা হয়, তবে Generics ব্যবহার করলে কম্পাইল টাইমে কিছুটা জটিলতা যোগ হয়।

  1. Generic Exception Class টাইপ-সেইফ এবং পুনঃব্যবহারযোগ্য এক্সেপশন হ্যান্ডলিংয়ের জন্য একটি শক্তিশালী সমাধান।
  2. এটি কাস্টম এক্সেপশন ক্লাসে ডাইনামিক এবং টাইপড ডেটা সংযুক্ত করার সুযোগ দেয়।
  3. জাভার Type Erasure মেকানিজম ব্যবহার করে এটি কম্পাইল টাইমে টাইপ যাচাই করে, কিন্তু রানটাইম ওভারহেড ছাড়াই কার্যকর হয়।
  4. বড় এবং ডাইনামিক অ্যাপ্লিকেশন ডেভেলপমেন্টের জন্য এটি একটি কার্যকর টুল।
Content added By

Generic Methods এবং Exception Propagation

350

Generic Methods জেনেরিক্স ব্যবহার করে এমন মেথড যা নির্দিষ্ট টাইপের উপর নির্ভর না করে কাজ করতে পারে। অন্যদিকে, Exception Propagation হল প্রোগ্রামে ত্রুটি (Exception) ঘটলে কীভাবে তা এক লেয়ার থেকে অন্য লেয়ারে পাঠানো হয়।


Generic Methods

Generic Method এমন একটি মেথড যেখানে মেথডের সিগনেচারে জেনেরিক টাইপ প্যারামিটার ডিফাইন করা হয়। এই প্যারামিটার টাইপ মেথড কল করার সময় নির্ধারিত হয়।

Generic Method এর কাঠামো:

public <T> ReturnType methodName(T parameter) {
    // method body
}

উদাহরণ: Generic Method ব্যবহার

public class GenericExample {
    // A generic method that prints any type of array
    public static <T> void printArray(T[] array) {
        for (T element : array) {
            System.out.print(element + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};
        String[] strArray = {"Java", "Generics", "Method"};

        printArray(intArray); // Output: 1 2 3 4 5
        printArray(strArray); // Output: Java Generics Method
    }
}

Key Points:

  1. printArray মেথডটি যেকোনো টাইপের অ্যারে প্রিন্ট করতে পারে।
  2. <T> হল জেনেরিক টাইপ প্যারামিটার।

Bounded Type Parameters সহ Generic Methods

মাঝে মাঝে মেথডে ব্যবহৃত টাইপ প্যারামিটারকে সীমাবদ্ধ করা প্রয়োজন। এ ক্ষেত্রে, bounded type parameters ব্যবহার করা হয়।

public class BoundedExample {
    // A generic method that calculates the sum of numbers
    public static <T extends Number> double sumArray(T[] array) {
        double sum = 0.0;
        for (T element : array) {
            sum += element.doubleValue();
        }
        return sum;
    }

    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};
        Double[] doubleArray = {1.1, 2.2, 3.3};

        System.out.println("Sum of Integer array: " + sumArray(intArray)); // Output: 15.0
        System.out.println("Sum of Double array: " + sumArray(doubleArray)); // Output: 6.6
    }
}

ব্যাখ্যা:

  • টাইপ প্যারামিটার <T extends Number> দিয়ে নিশ্চিত করা হয় যে, শুধুমাত্র Number ক্লাসের সাবটাইপ ব্যবহার করা যাবে।

Generic Methods এবং Exception Propagation

Generic Method থেকে Checked Exception Propagation

Generic Methods এর সাথে Exception Propagation করতে হলে Exception Type কে জেনেরিক টাইপ হিসেবে ডিফাইন করা যেতে পারে।

import java.util.concurrent.Callable;

public class ExceptionPropagationExample {
    // A generic method with exception propagation
    public static <T> T execute(Callable<T> task) throws Exception {
        return task.call();
    }

    public static void main(String[] args) {
        try {
            // Example of a successful execution
            String result = execute(() -> "Hello, Generics!");
            System.out.println(result);

            // Example of a failing execution
            execute(() -> {
                throw new Exception("Something went wrong!");
            });
        } catch (Exception e) {
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }
}

ব্যাখ্যা:

  1. execute মেথডটি Callable<T> ইন্টারফেস ব্যবহার করে জেনেরিক এক্সিকিউশন পরিচালনা করে।
  2. throws Exception ব্যবহার করে Checked Exception এর Propagation নিশ্চিত করা হয়।

Generic Methods এবং Custom Exceptions

Custom Exceptions এর মাধ্যমে জেনেরিক মেথডে নির্দিষ্ট ধরনের ত্রুটি পরিচালনা করা যায়।

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class CustomExceptionExample {
    public static <T> void validateNotNull(T value) throws CustomException {
        if (value == null) {
            throw new CustomException("Value cannot be null!");
        }
        System.out.println("Validated: " + value);
    }

    public static void main(String[] args) {
        try {
            validateNotNull("Java Generics"); // Valid input
            validateNotNull(null); // Will throw CustomException
        } catch (CustomException e) {
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }
}

Key Features:

  1. Custom Exception ব্যবহারের মাধ্যমে স্পষ্ট ত্রুটি পরিচালনা।
  2. জেনেরিক টাইপের জন্য ভ্যালিডেশন যুক্ত করা।

  1. Generic Methods কোডের পুনরায় ব্যবহারযোগ্যতা এবং টাইপ সেফটি বাড়ায়।
  2. Exception Propagation এর মাধ্যমে জেনেরিক মেথডে ত্রুটি পরিচালনা সহজ হয়।
  3. Custom Exceptions এবং Bounded Type Parameters জেনেরিক মেথডে আরও কার্যকর কাস্টমাইজেশন এবং টাইপ কন্ট্রোল প্রদান করে।
  4. প্রজেক্টের জটিলতার উপর নির্ভর করে Generic Methods এবং Exception Propagation ব্যবহার করা যায়।
Content added By
Promotion

Are you sure to start over?

Loading...