জাভায় 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 এর ক্ষেত্রে রয়েছে:
- Generics এর Type Erasure এর কারণে Checked Exceptions সরাসরি টাইপ প্যারামিটারে ব্যবহার করা যায় না।
- কম্পাইল টাইমে 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 এর ক্ষেত্রে:
- টাইপ সেফটি নিশ্চিত হয়।
- কাস্টম এক্সসেপশন ক্লাস তৈরি করা সহজ হয়।
- একাধিক টাইপের এক্সসেপশন সহজেই পরিচালনা করা যায়।
- এক্সসেপশন সংক্রান্ত ডেটা আরও সুনির্দিষ্ট ও পুনরায় ব্যবহারযোগ্য হয়।
যদিও Generics Checked Exceptions এর ক্ষেত্রে সরাসরি কার্যকর নয়, Unchecked Exceptions এবং কাস্টম হ্যান্ডলিংয়ে এটি শক্তিশালী সমাধান প্রদান করে।
Read more