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 defendant] could not shift his criminal liability,” Hui said. Read now The group’s featured image is of a Pepe frog yelling, often referred to as the “REEEEEEE” meme. Pepe the Frog was created back in 2005 by Matt Furie and has since become an internet symbol for meme culture and “degen” culture. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you: Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp.
from us


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