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 9168027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:32:00+00:00 2026-06-17T15:32:00+00:00

I have read articles upon articles trying to understand what exceptions are used for

  • 0

I have read articles upon articles trying to understand what exceptions are used for in php and I have gone through the answers already given in the forum. One of the answers which made atleast some sense to me is this one: Are exceptions in php really that useful?

Here is a simple function for finding the inverse of a integer with and without using exceptions (source):

With exception:

function inverse($x) {
  if ($x==0) { 
     throw new Exception('Division by zero.');
  } else { 
     return 1/$x;
  }
}    

try {
  inverse();
} 
catch (Exception $e) {
  echo $e->getMessage();
}

Without exception:

function inverse($x) {
  if ($x==0) { 
     echo "I'm zero. Don't let me be the denominator."; 
  } else { 
     return 1/$x;
  }
}    

So here is my question, why and when should I use one over the other?

  • 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-06-17T15:32:02+00:00Added an answer on June 17, 2026 at 3:32 pm

    There are various opinions on “when to use an exception”. My personal opinion is:

    • If you are working with your own code, theres basically no need to throw exceptions, as you then need to write your own handler for it – which also can be done without throwing an exception.
    • If you are developing APIs that other programmers are using, it can be usefull to throw exceptions, so the developer using your code knows, that he has to take care of handling errors, AND gets an idea of what was the error-reason. (instead of just getting null he might catch NumberToSmallException, NotANumberException, ….)

    In other words: When you already know how to handle an exception if it would appear – dont throw it. If the handling should be up to another developer, using your code – throw it.

    Exceptions should not be used to control the flow of your application logic. Therefore use if / else statements.

    But these are just my ten cents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying understand ViewModels deeper and I have read many articles and blogs
I have read through several articles which are alternatives to using setpixel/getpixel but I
I have read several articles/questions/forums discussing the best auto-complete plugin for jQuery. After trying
I have read various articles about the modal state that is used in the
I have read few articles about table partioning but still I am bit confused
I have read several articles and questions on concept of foreign key vs independent
I have read various articles about passing variables around using RMI. Some of them
I have read multiple articles about why singletons are bad. I know it has
I have read some articles on POCO in the enttity framework but still don't
I have read all the articles regarding img and srcs, buttons with autopostpack. Pretty

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.