Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:52+00:00 2026-05-23T09:55:52+00:00

I’ve been programming PHP for a long time, but not so much PHP 5…

  • 0

I’ve been programming PHP for a long time, but not so much PHP 5… I’ve known about exception handling in PHP 5 for some time, but never really looked into it. After a quick Google it seems fairly pointless to use exception handling – I can’t see the advantages of using it over just using some if() {} statements, and perhaps my own error handling class or whatever.

There’s got to be a bunch of good reasons for using it (I guess?!) otherwise it wouldn’t have been put into the language (probably). Can anyone tell me of some good benefits it has over just using a bunch of if statements or a switch statement or something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:55:52+00:00Added an answer on May 23, 2026 at 9:55 am

    Exceptions allow you to distinguish between different types of errors, and is also great for routing. For example…

    class Application
    {
        public function run()
        {
            try {
                // Start her up!!
            } catch (Exception $e) {
                // If Ajax request, send back status and message
                if ($this->getRequest()->isAjax()) {
                    return Application_Json::encode(array(
                        'status' => 'error',
                        'msg'    => $e->getMessage());
                }
    
                // ...otherwise, just throw error
                throw $e;
            }
        }
    }
    

    The thrown exception can then be handled by a custom error handler.

    Since PHP is a loosely typed language, you might need to ensure that only strings are passed as arguments to a class method. For example…

    class StringsOnly
    {
        public function onlyPassStringToThisMethod($string)
        {
            if (!is_string($string)) {
                throw new InvalidArgumentException('$string is definitely not a string');
            }
    
            // Cool string manipulation...
    
            return $this;
        }
    }
    

    …or if you need to handle different types of exceptions in different ways.

    class DifferentExceptionsForDifferentFolks
    {
        public function catchMeIfYouCan()
        {
            try {
                $this->flyForFree();
            } catch (CantFlyForFreeException $e) {
                $this->alertAuthorities();
                return 'Sorry, you can\'t fly for free dude. It just don\'t work that way!';
            } catch (DbException $e) {
                // Get DB debug info
                $this->logDbDebugInfo();
                return 'Could not access database. What did you mess up this time?';
            } catch (Exception $e) {
                $this->logMiscException($e);
                return 'I catch all exceptions for which you did not account!';
            }
        }
    }
    

    If using transactions in something like Zend Framework:

    class CreditCardController extends Zend_Controller_Action
    {
        public function buyforgirlfriendAction()
        {
            try {
                $this->getDb()->beginTransaction();
    
                $this->insertGift($giftName, $giftPrice, $giftWowFactor);
    
                $this->getDb()->commit();
            } catch (Exception $e) {
                // Error encountered, rollback changes
                $this->getDb()->rollBack();
    
                // Re-throw exception, allow ErrorController forward
                throw $e;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.