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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:05:25+00:00 2026-05-27T08:05:25+00:00

I am trying to catch an error on object creation, because this object can

  • 0

I am trying to catch an error on object creation, because this object can and should sometimes throw an error.

try {
    $obj = new MyObject();
} catch (Exception $e) {
    echo 'Caught exception: ';
}

I want to do a lot of things with this new object, but only IF it was created without throwing an exception.

The problem is that I do not wish to do all these things inside the try catch block. How would I accomplish this?

Thanks a lot
Michael

  • 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-27T08:05:25+00:00Added an answer on May 27, 2026 at 8:05 am

    I really can’t see any reason for what you are asking, but maybe the best thing is to do all the other stuff in a function that you call from the try/catch block…

    function allMyStuff($obj){
      // do some stuff to $obj here
    }
    
    try {
      $obj = new MyObject();
      allMyStuff($obj);
    } catch (Exception $e) {
      echo 'Caught exception: ';
    }
    

    Otherwise, to do literally as you seem to be asking, you could set a switch before the try/catch block to on, and set it to off in the catch block. That way you can test the switch to see whether to execute all your other stuff.

    $mySwitch = true;
    
    try {
      $obj = new MyObject();
    } catch (Exception $e) {
      echo 'Caught exception: ';
      $mySwitch = false;
    }
    
    if($mySwitch){
      // do some stuff here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to catch a ClassCastException when deserializing an object from xml. So, try
I am trying to catch 'Trying to get property of non-object' error with a
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
I’ve been trying to get this to work for a while but can’t (I
I am trying to update a user information and get the following error: Object
I am trying to post a Date object Date date = new Date() to
I've been trying this for a day now and I can't work it out.
I've been trying to solve this problem for over an hour now, and can't
I'm trying to load PNG images with ActionScript with a Loader object. This works
I'm trying to get around this error I get from Microsoft Visual Studio 10

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.