簡體   English   中英

嵌套的 try catch 捕獲相同的異常

[英]nested try catch catching the same exception

在嵌套的 try 和 catch 中是否有可能在所有塊中捕獲相同的異常?

例如:

try{
       try{
          throw new Exception("exception");
       }
       catch (Exception $exception)
       {
           echo "inner catch fires";
       }
    }
    catch (Exception $exception)
    {
        echo "outer catch fires";
    }

對於這種情況,結果將是“內部着火外部着火”

是的,您可以通過從內部捕獲中拋出異常來做到這一點。 如:

try {
    try {
        throw new Exception('exception');
    } catch (Exception $exception) {
        echo 'inner catch fires';

        throw $exception;
    }
} catch (Exception $exception) {
    echo 'outer catch fires';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM