Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to throw and catch exceptions with try - catch in PHP

1 Answer

0 votes
function div($a, $b) 
{
    if ($b == 0) 
        throw new Exception('Division by zero');
    return $a/$b;
}

try 
{
    echo div(10, 2) . "<br />";
    echo div(22, 0) . "<br />";
    echo div(60, 3) . "<br />";
} 
catch (Exception $e) 
{
    echo 'Exception: ' .  $e->getMessage() . '<br />';
}
 
 
echo "Countinue with the program"; 
 
/*
run:
 
5
Exception: Division by zero
Countinue with the program
 
*/

 



answered Jul 24, 2015 by avibootz
edited Jul 24, 2015 by avibootz

Related questions

3 answers 364 views
364 views asked Jan 3, 2016 by avibootz
2 answers 333 views
2 answers 328 views
1 answer 241 views
1 answer 215 views
215 views asked Dec 17, 2016 by avibootz
2 answers 399 views
2 answers 457 views
...