Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Add Exception Throwing to a Function | Custom Exceptions and Advanced Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
PHP Error Handling and Exceptions
SectionΒ 2. ChapterΒ 4
single

single

bookChallenge: Add Exception Throwing to a Function

Swipe to show menu

Task

Swipe to start coding

Update the calculateDivision function to throw exceptions for invalid data, and ensure the main program handles these exceptions using try/catch blocks.

  • If either $numerator or $denominator is not numeric, throw an InvalidArgumentException.
  • If $denominator is zero, throw a DivisionByZeroError.
  • If both arguments are valid and the denominator is not zero, return the result of dividing $numerator by $denominator.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt