Try Catch Finally
Execute a catch.
A try/catch/finally block with optional finally. Exceptions are not typed, so you can only have one catch. The finallyblock is executed however the block finished - either normally or after the catch has been executed. Abort can be caught.
try {
// code that can raise an exception
if ($a = 1) {
echo "a is 1 – bailing out";
abort; // this will raise an exception
}
echo "a was not 1";
} catch(ex) {
// ex will be ABORT (this is the exception)
echo "We aborted (a was 1)!";
} finally {
// this code always executes
echo "a was 1 or something else";
}
Here is an example of catching a parse error when converting an XML document into an Array. If the try/catch were not in place, the parse error would throw an exception and end the script.
set test="<a><b>hello</b><b>goodbye</b>";
try {
// This will fail as there is no closing </a> tag.
set jsonvals = ${test.xmlparse()};
} catch(ex) {
echo "exception caught!";
echo $ex;
}
echo "This code executes regardless of a parse error or not";
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.
Last modified August 31, 2024: Signed-off-by: tracyragan <[email protected]> (20379ca)