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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:44:52+00:00 2026-06-11T01:44:52+00:00

I’m just implementing Exceptions in to my code for the first time. For exceptions

  • 0

I’m just implementing Exceptions in to my code for the first time.

For exceptions that are used within a class function, where is best to define the exception?

Here’s a class and a few functions that represent a piece of code that I have written:

//
// Do I define my exception here?
//
class CreateQuizException extends Exception {}

class Quiz()
{

    // Define the items in my quiz object
    $quiz_id = null;
    $quiz_name = null;

    // Function to create a quiz record in the database
    function CreateQuizInDatabase()
    {
        //
        // OR DO I DEFINE IT WITHIN THE FUNCTION??
        //
        class CreateQuizException extends Exception {}

        try
        {

            // Insert the record in to the database
            $create_quiz = mysql_query("INSERT INTO quizzes (quiz_name) VALUES ("' . $this->quiz_name . '")");

            if (!$create_quiz)
            {
                // There was an error creating record in the database
                throw new CreateQuizException("Failed inserting record");
            }
            else
            {
                // Return true, the quiz was created successfully
                return true;
            }

        }
        catch (CreateQuizException $create_quiz_exception)
        {
            // There was an error creating the quiz in the database
            return false;
        }
        catch (Exception $other_exception)
        {
            // There was another error
        }
    }

}

Is it:

  • Before the class definition that the exception will be used in
  • Only Within the function that it will be used in
  • Or somewhere else that I haven’t thought of?
  • 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-11T01:44:53+00:00Added an answer on June 11, 2026 at 1:44 am

    Typically I’ve seen and used two places for Exception placement for two distinct reasons:

    1. Outside of the class definition, since is it possible that the Exception will be thrown to the caller and I want to be explicit that this Exception is in the public namespace. It might be useful to note that I really only see Exceptions placed at the end of the class file in which they’re defined, but this is preference and has no functional significance.

      class Foo {
      }
      
      class FooException extends Exception {}
      
    2. Inside of the class definition, but outside of the method definitions, if the Exception is local to this class, not relevant outside of the class, and not going to be handled by the caller. Typically, the private access modifier is introduced to enforce this.

      class Foo {
          public function bar() {
          }
          private class FooException extends Exception {}
      }
      

      However, I do not believe that PHP supports nested classes, as I cannot get one example of nested classes to parse. I am drawing my examples more from Java than PHP.

    Placing Exception definitions inside methods seems too granular, you’ll end up polluting your code with Exceptions and end up with:

    class MyFunctionReturnedABadValueAndNowImSad extends Exception {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.