Ody*_*3us 10 php exception try-catch
我在一个主try catch语句中嵌套try catch,我想知道如果其中一个嵌套的try catch失败,我怎么能使主try catch失败?
这是我的代码:
try
{
try
{
//how can I make the main try catch fail if this try catch fails?
}
catch(Exception $e)
{
error_log();
}
}
catch(Exception $e)
{
error_log();
}
Run Code Online (Sandbox Code Playgroud)
您应该为各种不同类型的Exception扩展Exception.这样你就可以触发一个特定的try-catch块:
try
{
...
try
{
throwSomeException();
}
catch ( InnerException $e )
{
...do stuff only for InnerException...
}
...
}
catch ( Exception $e )
{
...do stuff for all types of exception...
}
Run Code Online (Sandbox Code Playgroud)
此外,您可以链接catch语句以在单个try-catch中触发不同的块:
try
{
...
}
catch ( SpecificTypeOfException $e )
{
..do something specific
}
catch ( TypeOfException $e )
{
..do something less specific
}
catch ( Exception $e )
{
..do something for all exceptions
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8750 次 |
| 最近记录: |