JAVAPRO_IR Telegram 4514
⚠️ Exception Handling در CompletableFuture

یکی از چالش‌های بزرگ در برنامه‌نویسی Async اینه که خطاها به راحتی گم می‌شن.
ولی جاوا ابزارهای خیلی خوبی برای مدیریت Exception توی CompletableFuture داده.

مثال ۱: handle()
import java.util.concurrent.CompletableFuture;

public class HandleExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            if (true) throw new RuntimeException("خطا در پردازش!");
            return "موفقیت";
        }).handle((result, ex) -> {
            if (ex != null) {
                return "خطا مدیریت شد: " + ex.getMessage();
            }
            return result;
        });

        System.out.println(future.join());
    }
}

🔎 توضیح:

handle :
هم نتیجه و هم خطا رو می‌گیره.

اگه خطا اتفاق بیفته، می‌تونیم یه مقدار جایگزین برگردونیم.

مثال ۲: exceptionally()
import java.util.concurrent.CompletableFuture;

public class ExceptionallyExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            throw new RuntimeException("مشکل پیش اومد!");
        }).exceptionally(ex -> {
            return "خطا: " + ex.getMessage();
        });

        System.out.println(future.join());
    }
}

📌 exceptionally :
فقط وقتی خطا رخ بده صدا زده می‌شه.

مثال ۳: whenComplete()
import java.util.concurrent.CompletableFuture;

public class WhenCompleteExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            if (Math.random() > 0.5) {
                throw new RuntimeException("خطای شانسی!");
            }
            return "کار موفق بود!";
        }).whenComplete((result, ex) -> {
            if (ex != null) {
                System.out.println(" خطا: " + ex.getMessage());
            } else {
                System.out.println(" نتیجه: " + result);
            }
        });

        // برای اینکه برنامه منتظر بمونه
        future.join();
    }
}

📌 whenComplete :
بیشتر برای Log گرفتن یا کارهای جانبی استفاده می‌شه، چون نتیجه اصلی Future رو تغییر نمی‌ده.

🎯 جمع‌بندی:

handle() →
گرفتن همزمان نتیجه یا خطا.
exceptionally() →
فقط برای مدیریت خطا.
whenComplete() →
مثل finally عمل می‌کنه (همیشه اجرا می‌شه).

این متدها کمک می‌کنن که توی برنامه‌های Async، خطاها گم نشن و بتونیم به درستی مدیریت‌شون کنیم.

#کاربرـحرفهـای



🆔 @javapro_ir
🆔 @group_javapro
👍21💯1



tgoop.com/javapro_ir/4514
Create:
Last Update:

⚠️ Exception Handling در CompletableFuture

یکی از چالش‌های بزرگ در برنامه‌نویسی Async اینه که خطاها به راحتی گم می‌شن.
ولی جاوا ابزارهای خیلی خوبی برای مدیریت Exception توی CompletableFuture داده.

مثال ۱: handle()

import java.util.concurrent.CompletableFuture;

public class HandleExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            if (true) throw new RuntimeException("خطا در پردازش!");
            return "موفقیت";
        }).handle((result, ex) -> {
            if (ex != null) {
                return "خطا مدیریت شد: " + ex.getMessage();
            }
            return result;
        });

        System.out.println(future.join());
    }
}

🔎 توضیح:

handle :
هم نتیجه و هم خطا رو می‌گیره.

اگه خطا اتفاق بیفته، می‌تونیم یه مقدار جایگزین برگردونیم.

مثال ۲: exceptionally()
import java.util.concurrent.CompletableFuture;

public class ExceptionallyExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            throw new RuntimeException("مشکل پیش اومد!");
        }).exceptionally(ex -> {
            return "خطا: " + ex.getMessage();
        });

        System.out.println(future.join());
    }
}

📌 exceptionally :
فقط وقتی خطا رخ بده صدا زده می‌شه.

مثال ۳: whenComplete()
import java.util.concurrent.CompletableFuture;

public class WhenCompleteExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            if (Math.random() > 0.5) {
                throw new RuntimeException("خطای شانسی!");
            }
            return "کار موفق بود!";
        }).whenComplete((result, ex) -> {
            if (ex != null) {
                System.out.println(" خطا: " + ex.getMessage());
            } else {
                System.out.println(" نتیجه: " + result);
            }
        });

        // برای اینکه برنامه منتظر بمونه
        future.join();
    }
}

📌 whenComplete :
بیشتر برای Log گرفتن یا کارهای جانبی استفاده می‌شه، چون نتیجه اصلی Future رو تغییر نمی‌ده.

🎯 جمع‌بندی:

handle() →
گرفتن همزمان نتیجه یا خطا.
exceptionally() →
فقط برای مدیریت خطا.
whenComplete() →
مثل finally عمل می‌کنه (همیشه اجرا می‌شه).

این متدها کمک می‌کنن که توی برنامه‌های Async، خطاها گم نشن و بتونیم به درستی مدیریت‌شون کنیم.

#کاربرـحرفهـای



🆔 @javapro_ir
🆔 @group_javapro

BY برنامه نویسی جاوا | Java


Share with your friend now:
tgoop.com/javapro_ir/4514

View MORE
Open in Telegram


Telegram News

Date: |

The Channel name and bio must be no more than 255 characters long Clear The group also hosted discussions on committing arson, Judge Hui said, including setting roadblocks on fire, hurling petrol bombs at police stations and teaching people to make such weapons. The conversation linked to arson went on for two to three months, Hui said. Telegram has announced a number of measures aiming to tackle the spread of disinformation through its platform in Brazil. These features are part of an agreement between the platform and the country's authorities ahead of the elections in October. The best encrypted messaging apps
from us


Telegram برنامه نویسی جاوا | Java
FROM American