জাভার Generics এবং Exception Handling একত্রে ব্যবহারে কিছু নিয়ম এবং সীমাবদ্ধতা রয়েছে। জেনেরিক্স টাইপ নিরাপত্তা এবং কোড পুনঃব্যবহারযোগ্যতা বাড়ায়, তবে একে Exception এর সাথে ব্যবহার করার সময় সতর্ক হতে হয়, বিশেষত Type Erasure এর প্রভাবের কারণে।
Generics এবং Exception Handling: গুরুত্বপূর্ণ পয়েন্টসমূহ
- জেনেরিক ক্লাস বা মেথড থেকে Checked Exception থ্রো করা যাবে।
- Generics এর সাথে Exception Subtyping এবং Catching সঠিকভাবে পরিচালনা করতে হবে।
- জেনেরিক টাইপ প্যারামিটার Exception এর টাইপ হিসেবে ব্যবহার করা যায় না।
- 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
- Checked Exception এর সাথে জেনেরিক্স ব্যবহার করলে সাবধানে টাইপ ডিক্লেয়ার করুন।
উদাহরণ:
public class GenericHandler<T extends Exception> { public void handleException(T exception) throws T { throw exception; } }
- Throwable বা Exception এর Subclass কে জেনেরিক টাইপ হিসেবে ব্যবহারের চেষ্টা করুন।
যেমন:
public class CustomHandler<T extends Throwable> { public void process(T throwable) throws T { throw throwable; } }
- Generic Method ব্যবহার করুন যেখানে Exception টাইপ ডাইনামিক হতে পারে।
উদাহরণ:
public static <T extends Throwable> void rethrow(Throwable throwable) throws T { throw (T) throwable; }
- Wildcards (
?) ব্যবহার করুন যখন Exception টাইপ ফিক্সড নয়। - জেনেরিক Exception ক্লাস তৈরির সময় যথাযথ কাস্টমাইজেশন করুন।
জেনেরিক্স এবং Exception Handling একত্রে ব্যবহার:
- টাইপ সেফটি নিশ্চিত করে।
- কোড রিইউজেবিলিটি বাড়ায়।
- Runtime Exception এর ঝুঁকি কমায়।
তবে Type Erasure এবং জাভার Exception Handling Mechanism এর কারণে কিছু সীমাবদ্ধতা রয়েছে, যা সচেতনভাবে এড়িয়ে যাওয়া উচিত। সঠিকভাবে ব্যবহারে জেনেরিক্স এবং Exception Handling এর মিশ্রণ অ্যাপ্লিকেশন ডেভেলপমেন্টে একটি শক্তিশালী সমাধান দিতে পারে।
জাভায় 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 এবং কাস্টম হ্যান্ডলিংয়ে এটি শক্তিশালী সমাধান প্রদান করে।
জাভার জেনেরিক্স ব্যবহারের সময় Checked এবং Unchecked Exceptions নিয়ে কিছু চ্যালেঞ্জ এবং সীমাবদ্ধতা রয়েছে। এই চ্যালেঞ্জগুলো কীভাবে সমাধান করা যায় এবং Generics কীভাবে Exception Handling এর ক্ষেত্রে কার্যকর হয়, তা আমরা এই আলোচনায় তুলে ধরব।
Exception: Checked এবং Unchecked ব্যাখ্যা
- Checked Exceptions:
- কম্পাইল টাইমে চেক করা হয়।
- উদাহরণ:
IOException,SQLException। - মেথডের সিগনেচারে throws দিয়ে ডিক্লেয়ার করা বাধ্যতামূলক।
- 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 এর সুবিধা
- Type-Safe Handling: Checked এবং Unchecked Exceptions এর ক্ষেত্রে টাইপ-সেফটি নিশ্চিত করা যায়।
- Reusable Code: Generics ব্যবহার করে একই Exception Handling কোড পুনঃব্যবহার করা যায়।
- Runtime Errors Minimized: কম্পাইল টাইমে Checked Exceptions চেক করা হয়, যা রানটাইম ত্রুটি কমায়।
- 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!
}
}
}
- Generics এবং Exception Handling একত্রে ব্যবহারে Checked এবং Unchecked Exceptions টাইপ-সেফ এবং পুনঃব্যবহারযোগ্যভাবে পরিচালনা করা যায়।
- Generics এর সীমাবদ্ধতা (যেমন Type Erasure) থাকার পরেও Helper Method এবং Custom Design ব্যবহার করে কার্যকর সমাধান সম্ভব।
- Checked Exceptions এর ক্ষেত্রে Generics ব্যবহার ডিজাইনের সময় আরও বেশি নিরাপত্তা এবং ফ্লেক্সিবিলিটি প্রদান করে।
জাভাতে Generic Exception Class তৈরি করে ডাইনামিক এবং পুনঃব্যবহারযোগ্য এক্সেপশন মেকানিজম তৈরি করা যায়। সাধারণত, এটি ব্যবহার করা হয় এমন অবস্থায় যেখানে এক্সেপশন টাইপের সাথে কিছু অতিরিক্ত টাইপ-ডেটা যুক্ত করতে হয়। Generic Exception Class টাইপ-সেইফটি নিশ্চিত করে এবং কোডের পুনঃব্যবহারযোগ্যতা বাড়ায়।
Generic Exception Class তৈরির উপকারিতা
- টাইপ-সেইফ এক্সেপশন হ্যান্ডলিং: কম্পাইল টাইমে টাইপ যাচাই করা যায়।
- ডাইনামিক ডেটা যোগ করা: এক্সেপশন ক্লাসে অতিরিক্ত টাইপযুক্ত ডেটা যোগ করা যায়।
- Reusable Design: একবার তৈরি করলে এটি বিভিন্ন প্রজেক্টে পুনঃব্যবহার করা যায়।
- কোড সহজ ও পরিষ্কার করা: কমন এক্সেপশন মেকানিজম তৈরি করে কোড কমপ্লেক্সিটি কমানো যায়।
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 এর সীমাবদ্ধতা
- Checked Exception: Generics ব্যবহার করে শুধুমাত্র Checked Exception তৈরি করা সম্ভব।
- Type Erasure: Generics এর টাইপ ইনফরমেশন Type Erasure এর মাধ্যমে কম্পাইল টাইমে মুছে ফেলা হয়, যা কিছু ক্ষেত্রে সীমাবদ্ধতা আনতে পারে।
- Performance Impact: যদিও টাইপ ইনফরমেশন মুছে ফেলা হয়, তবে Generics ব্যবহার করলে কম্পাইল টাইমে কিছুটা জটিলতা যোগ হয়।
- Generic Exception Class টাইপ-সেইফ এবং পুনঃব্যবহারযোগ্য এক্সেপশন হ্যান্ডলিংয়ের জন্য একটি শক্তিশালী সমাধান।
- এটি কাস্টম এক্সেপশন ক্লাসে ডাইনামিক এবং টাইপড ডেটা সংযুক্ত করার সুযোগ দেয়।
- জাভার Type Erasure মেকানিজম ব্যবহার করে এটি কম্পাইল টাইমে টাইপ যাচাই করে, কিন্তু রানটাইম ওভারহেড ছাড়াই কার্যকর হয়।
- বড় এবং ডাইনামিক অ্যাপ্লিকেশন ডেভেলপমেন্টের জন্য এটি একটি কার্যকর টুল।
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:
printArrayমেথডটি যেকোনো টাইপের অ্যারে প্রিন্ট করতে পারে।<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());
}
}
}
ব্যাখ্যা:
executeমেথডটিCallable<T>ইন্টারফেস ব্যবহার করে জেনেরিক এক্সিকিউশন পরিচালনা করে।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:
- Custom Exception ব্যবহারের মাধ্যমে স্পষ্ট ত্রুটি পরিচালনা।
- জেনেরিক টাইপের জন্য ভ্যালিডেশন যুক্ত করা।
- Generic Methods কোডের পুনরায় ব্যবহারযোগ্যতা এবং টাইপ সেফটি বাড়ায়।
- Exception Propagation এর মাধ্যমে জেনেরিক মেথডে ত্রুটি পরিচালনা সহজ হয়।
- Custom Exceptions এবং Bounded Type Parameters জেনেরিক মেথডে আরও কার্যকর কাস্টমাইজেশন এবং টাইপ কন্ট্রোল প্রদান করে।
- প্রজেক্টের জটিলতার উপর নির্ভর করে Generic Methods এবং Exception Propagation ব্যবহার করা যায়।
Read more