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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:30:49+00:00 2026-05-26T04:30:49+00:00

I have a project object. A user can assign a worker object for it.

  • 0

I have a project object.
A user can assign a worker object for it.

99% of the case the project object has all the proper fields set.
In some cases, the project is missing a field.

In order for a worker to be assign to a project the project must have all the required fields setup.

To solve this, I throw exceptions like this: ( don’t try to find a pattern here, the real example is more complex )


if ($project->startDate == false ) {
  throw new Exception("Missing startDate attribute for project id: $id");
}


if ($project->finishDate == false ) {
  throw new Exception("Missing finishDate attribute for project id: $id");
}

if ($project->startDate > $project->finishDate ) {
  throw new Exception("Invalid start date for project id: $id");
}

The problem with this is that I need to display a custom message to the user each time. For example if the first error is thrown the user should see: ‘Please setup the project start date’ and so on.

How can I do this ?

  • 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-26T04:30:50+00:00Added an answer on May 26, 2026 at 4:30 am

    Just define your own exception class and your whole code in a try .. catch:

    class FormException extends Exception {
       private var $userMessage;
       public function __construct($message, $userMessage) {
         parent::__construct($message);
         $this->userMessage = $userMessage;
       }
       public function getUserMessage() {return $this->userMessage;}
    }
    
    try {
      // Whole code goes here, probably a function call
      throw new FormException("Missing startDate attribute for project id: $id",
                              'Please setup the project start date');
    } catch (FormException $e) {
      echo $e->getUserMessage();
      error_log($e->getMessage());
    }
    

    By the way, if you want to include variable contents in a string, either use double quotes ("id: $id") or concatenate ('id: ' . $id).

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

Sidebar

Related Questions

Background Each Project has one Account it is associated with. Each User can have
I have some code in my project that saves an object to the database,
In our project we need to store some object (User, for example) and also
IN hibernate JPA how can I update of a object? I have one Project
I have a project where the user can add multiple files to a cart
I have a project that requires user-defined attributes for a particular object at runtime
I have a project which exposes object model to use by different types of
I have a business object project, which contains composite structure: public class Tree {
I have a Browser Helper Object project in c# that (obviously) references shdocvw.dll. I
I have been working on keeping things object oriented for my project. Currently, I'm

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.