Bird
0
0

Which of the following is the correct syntax for using a finally block in PHP?

easy📝 Syntax Q12 of 15
PHP - Error and Exception Handling
Which of the following is the correct syntax for using a finally block in PHP?
Atry { /* code */ } finally (Exception $e) { /* cleanup */ }
Btry { /* code */ } catch (Exception $e) { /* handle */ } finally() { /* cleanup */ }
Ctry { /* code */ } catch { /* handle */ } finally { /* cleanup */ }
Dtry { /* code */ } catch (Exception $e) { /* handle */ } finally { /* cleanup */ }
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP try-catch-finally syntax

    The correct syntax requires try, then catch with exception parameter, then finally without parameters or parentheses.
  2. Step 2: Check each option for syntax errors

    try { /* code */ } catch (Exception $e) { /* handle */ } finally { /* cleanup */ } matches correct syntax. try { /* code */ } finally (Exception $e) { /* cleanup */ } wrongly uses parameters in finally. try { /* code */ } catch { /* handle */ } finally { /* cleanup */ } misses exception variable in catch. try { /* code */ } catch (Exception $e) { /* handle */ } finally() { /* cleanup */ } wrongly uses parentheses after finally.
  3. Final Answer:

    try { /* code */ } catch (Exception $e) { /* handle */ } finally { /* cleanup */ } -> Option D
  4. Quick Check:

    Correct try-catch-finally syntax = B [OK]
Quick Trick: finally block has no parameters or parentheses [OK]
Common Mistakes:
  • Adding parameters to finally block
  • Omitting exception variable in catch
  • Using parentheses after finally keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes