rfc:suppressed_exceptions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:suppressed_exceptions [2019/04/04 16:49] – Initial version danack | rfc:suppressed_exceptions [2019/04/04 17:43] (current) – danack | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | In some scenarios | + | Currently in PHP there are some scenarios |
<code php> | <code php> | ||
function fetchDataOverNetwork() { | function fetchDataOverNetwork() { | ||
- | |||
- | $networkExceptions = []; | ||
for ($i = 0; $i < MAX_ATTEMPTS; | for ($i = 0; $i < MAX_ATTEMPTS; | ||
try { | try { | ||
- | | + | |
- | | + | return foo(); |
+ | } | ||
+ | catch (NetworkException $networkException) { | ||
+ | // can't do anything with $networkException here | ||
} | } | ||
- | catch (NetworkException $ne) { | ||
- | $networkExceptions[] = $ne; | ||
- | } | ||
} | } | ||
| | ||
Line 39: | Line 37: | ||
} | } | ||
catch (FooException $fooException) { | catch (FooException $fooException) { | ||
- | | + | |
- | // try to release resource cleanly | + | // try to release resource cleanly |
- | $resource-> | + | $resource-> |
- | throw $fooException; | + | throw $fooException; |
- | } | + | } |
- | catch (ResourceException $resourceException) { | + | catch (ResourceException $resourceException) { |
- | // The information about $resourceException is lost. | + | // The information about $resourceException is lost. |
- | throw $fooException; | + | throw $fooException; |
- | } | + | } |
} | } | ||
Line 65: | Line 63: | ||
<code php> | <code php> | ||
- | public void addSuppressed(Throwable exception) | + | public void addSuppressed(Throwable exception); |
public getSuppressed(): | public getSuppressed(): | ||
Line 86: | Line 84: | ||
for ($i = 0; $i < MAX_ATTEMPTS; | for ($i = 0; $i < MAX_ATTEMPTS; | ||
try { | try { | ||
- | | + | |
- | | + | return foo(); |
+ | } | ||
+ | catch (NetworkException $ne) { | ||
+ | $networkExceptions[] = $ne; | ||
} | } | ||
- | catch (NetworkException $ne) { | ||
- | $networkExceptions[] = $ne; | ||
- | } | ||
} | } | ||
Line 97: | Line 95: | ||
| | ||
foreach ($networkExceptions as $networkException) { | foreach ($networkExceptions as $networkException) { | ||
- | | + | |
} | } | ||
| | ||
Line 113: | Line 111: | ||
} | } | ||
catch (FooException $fooException) { | catch (FooException $fooException) { | ||
- | | + | |
- | // try to release resource cleanly | + | // try to release resource cleanly |
- | $resource-> | + | $resource-> |
- | throw $fe; | + | throw $fe; |
- | } | + | } |
- | catch (ResourceException $resourceException) { | + | catch (ResourceException $resourceException) { |
- | $fe-> | + | $fe-> |
- | throw $fooException; | + | throw $fooException; |
- | } | + | } |
} | } | ||
</ | </ | ||
Line 129: | Line 127: | ||
As per the resource exception sometimes it is necessary to add suppressed exception to an exception that has been caught and is going to be re-thrown. | As per the resource exception sometimes it is necessary to add suppressed exception to an exception that has been caught and is going to be re-thrown. | ||
+ | |||
+ | |||
+ | ===== Why not just use the ' | ||
+ | |||
+ | The constructor for Exceptions allows a ' | ||
+ | |||
+ | <code php> | ||
+ | function foo() | ||
+ | { | ||
+ | try { | ||
+ | bar(); | ||
+ | } | ||
+ | // LogicException is part of core | ||
+ | catch (LogicException $le) { | ||
+ | throw new FooException( | ||
+ | " | ||
+ | 0, | ||
+ | $le | ||
+ | ) | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In this example, only one thing has gone wrong unexpectedly and so the FooException and LogicException are representing a single unexpected error. As it is a single error, this exception only needs to be logged once. | ||
+ | |||
+ | In the resource exception example, the fact that there was an exception calling ' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Additionally, | ||
+ | |||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
rfc/suppressed_exceptions.1554396577.txt.gz · Last modified: 2019/04/04 16:49 by danack