第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

拋出異常的Java 8 Lambda函數(shù)?

拋出異常的Java 8 Lambda函數(shù)?

一只萌萌小番薯 2019-06-25 16:28:21
拋出異常的Java 8 Lambda函數(shù)?我知道如何創(chuàng)建對(duì)具有String參數(shù)并返回int它是:Function<String, Integer>但是,如果函數(shù)拋出異常,則此操作不起作用,例如,它被定義為:Integer myMethod(String s) throws IOException我將如何定義這個(gè)引用?
查看完整描述

3 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要執(zhí)行以下操作之一。

  • 如果是您的代碼,那么定義您自己的函數(shù)接口來(lái)聲明選中的異常:

    @FunctionalInterfacepublic interface CheckedFunction<T, R> {
       R apply(T t) throws IOException;}

    并使用它:

    void foo (CheckedFunction f) { ... }
  • 否則,包裝Integer myMethod(String s)在不聲明選中異常的方法中:

    public Integer myWrappedMethod(String s) {
        try {
            return myMethod(s);
        }
        catch(IOException e) {
            throw new UncheckedIOException(e);
        }}

    然后:

    Function<String, Integer> f = (String t) -> myWrappedMethod(t);

    或:

    Function<String, Integer> f =
        (String t) -> {
            try {
               return myMethod(t);
            }
            catch(IOException e) {
                throw new UncheckedIOException(e);
            }
        };


查看完整回答
反對(duì) 回復(fù) 2019-06-25
?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

實(shí)際上你可以擴(kuò)展Consumer(和Function(等等)有一個(gè)處理異常的新接口-使用Java 8的默認(rèn)方法!

考慮這個(gè)接口(擴(kuò)展Consumer):

@FunctionalInterfacepublic interface ThrowingConsumer<T> extends Consumer<T> {

    @Override
    default void accept(final T elem) {
        try {
            acceptThrows(elem);
        } catch (final Exception e) {
            // Implement your own exception handling logic here..
            // For example:
            System.out.println("handling an exception...");
            // Or ...
            throw new RuntimeException(e);
        }
    }

    void acceptThrows(T elem) throws Exception;}

然后,例如,如果您有一個(gè)列表:

final List<String> list = Arrays.asList("A", "B", "C");

如果你想吃的話。帶著forEach)使用一些拋出異常的代碼,您通常會(huì)設(shè)置一個(gè)try/catch塊:

final Consumer<String> consumer = aps -> {
    try {
        // maybe some other code here...
        throw new Exception("asdas");
    } catch (final Exception ex) {
        System.out.println("handling an exception...");
    }};list.forEach(consumer);

但是使用這個(gè)新接口,您可以使用lambda表達(dá)式實(shí)例化它,編譯器不會(huì)抱怨:

final ThrowingConsumer<String> throwingConsumer = aps -> {
    // maybe some other code here...
    throw new Exception("asdas");};list.forEach(throwingConsumer);

甚至只是為了更簡(jiǎn)潔?。?/trans>

list.forEach((ThrowingConsumer<String>) aps -> {
    // maybe some other code here...
    throw new Exception("asda");});

更新:看起來(lái)有一個(gè)非常好的實(shí)用程序庫(kù)榴蓮錯(cuò)誤它可以用更多的靈活性來(lái)解決這個(gè)問(wèn)題。例如,在上面的實(shí)現(xiàn)中,我顯式地定義了錯(cuò)誤處理策略(System.out...throw RuntimeException),而Durian的錯(cuò)誤允許您通過(guò)一組大型實(shí)用方法動(dòng)態(tài)地應(yīng)用策略。謝謝分享它,@NedTwigg!

樣本使用情況:

list.forEach(Errors.rethrow().wrap(c -> somethingThatThrows(c)));


查看完整回答
反對(duì) 回復(fù) 2019-06-25
  • 3 回答
  • 0 關(guān)注
  • 1328 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)