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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:06:07+00:00 2026-05-16T12:06:07+00:00

i am trying to understand the use of exceptions in PHP.. How they work

  • 0

i am trying to understand the use of exceptions in PHP.. How they work and when to use them… The basic structure below, is the the correct method.. It seems somewhat bloated to me?

Thanks in advance for any advise or help..

<?php

class APIException extends Exception 
{
    public function __construct($message)
    {
        parent::__construct($message, 0);
    }

    public function __toString()
   {
        echo('Error: ' . parent::getMessage());
    }

   public function __toDeath()  
   {
      die("Oops: ". parent::getMessage());
   }
}

?>

<?php

require_once('exception.class.php');

class auth extends base
{
   /* 
    * User functions 
    */ 

   public function user_create($args='')
   {
      try
      {
         if ( !isset($arg['username']) || !isset($arg['password']) || 
              !isset($arg['question']) || !isset($arg['answer']) )
         {
            throw new APIException('missing variables, try again');
         }
      }
      catch (APIException $e)
      {
         $e->__toString();
      }

      try
      {
         if ( $this->user_exists() ) 
         {
            throw new APIException('user already exists');
         }
      }
      catch (APIException $e)
      {
         $e->__toString();
      }
   }

   protected function user_exists($name)
   {
      // Do SQL Query 
      try
      {
         $sql = "select * from user where username='$user'";
         if (!mysql_query($sql))
         {
            throw new APIException('SQL ERROR');
         }
      }
      catch (APIException $e)
      {
         $e->__toDeath();
      }
   }
}

?>
  • 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-16T12:06:07+00:00Added an answer on May 16, 2026 at 12:06 pm

    No, you’re not using them correctly. Take a look at this

    public function user_create($args='')
    {
      try
      {
         if ( !isset($arg['username']) || !isset($arg['password']) || 
              !isset($arg['question']) || !isset($arg['answer']) )
         {
            throw new APIException('missing variables, try again');
         }
      }
      catch (APIException $e)
      {
         $e->__toString();
      }
      //snip...
    

    When you throw new APIException('missing variables, try again');, it will just be caught by catch (APIException $e).

    One of the points of exceptions is so that you can tell code calling the function that something went wrong. A more proper usage might look something like this.

    public function user_create($args='')
    {
       if ( !isset($arg['username']) || !isset($arg['password']) || 
            !isset($arg['question']) || !isset($arg['answer']) )
       {
          throw new APIException('missing variables, try again');
       }
      //snip...
    
    // elsewhere
    try {
      $foo->user_create('bar');
    } catch(APIException $e) {
      // handle error here
      // log_error($e->getCode(), $e->getMessage());
    }
    

    Notice that the you use the try/catch block when you call user_create. Not within user_create.

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

Sidebar

Related Questions

I am learning Django and I am trying to understand the use of models.py
I'm trying to understand when to use self.method_name vs. when to use Classname.method_name. In
I'm trying to understand how to use firebug to debug my Javascript. So I
I am trying to understand how to use instance variable in Perl OO -
I am trying to understand how to use reference parameters. There are several examples
I am trying to understand my embedded Linux application's memory use. The /proc/pid/maps utility/file
I am trying to better understand when I should and should not use Iterators.
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm trying to use the new Java 7 Files.createSymbolicLink() method within Play! Framework, and
I'm trying to understand how to use PDO with a connection class. class db

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.