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

  • Home
  • SEARCH
  • 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 4592592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:33:39+00:00 2026-05-21T22:33:39+00:00

I am trying to understand what the best approach would be to handle Exceptions

  • 0

I am trying to understand what the best approach would be to handle Exceptions in the following scenario:

I have a class employee:

class employee extends person {
    private $salary;
    private $baseSalary = 6.5;

    function __construct($f, $m, $l, $a,$fsalary=0){
        if(!is_numeric($fsalary)){
            throw new Exception("Age supplied is not a number", 114);
        }
        parent::__construct($f, $m, $l, $a);    
        $this->salary=$fsalary;
    }
    function GetDetails(){
         return parent::GetName().
                "<br/>".
                $this->salary;
    }
    function __toString(){
        return $this->GetDetails();
    }

}

And using this:

try{
    if(!$f = new employee("Sarah", "Sebastian", "Pira", "abc")){
        throw new Exception();
    }
    else {
        echo $f;        
    }

}
catch (Exception $e){
    echo "<br/>";
    echo var_dump($e);
}

Now I would think it would be a good idea to throw an exception in the class and then use just one catch block in all the scripts that would be using an employee object – But this doesn’t seem to work – I need to have a try catch block within the class – Is this the correct way of looking at this?

Thanks

  • 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-21T22:33:40+00:00Added an answer on May 21, 2026 at 10:33 pm

    I think what you’re saying is that you want to do something like this:

    try {
        class Employee extends Person {
            // ...blah blah...
        }
    }
    catch(Exception $e) {
        // handle exception
    }
    

    …and then be able to insantiate it in other classes, without explicitly catching any exceptions:

    // try { << this would be removed
        $employee = new Employee();
    // }
    // catch(Exception $e) {
    //    (a whole bunch of code to handle the exception here)
    // }
    

    You can’t do that, because then the try/catch block in the class will only catch any exceptions that occur when defining the class. They won’t be caught when you try to instantiate it because your new Employee line is outside the try/catch block.

    So really, your problem is that you want to be able to re-use a try/catch block in multiple places without re-writing the code. In that case, your best solution is to move the contents of the catch block out to a separate function that you can call as necessary. Define the function in the Employee class file and call it like this:

    try {
         $employee = new Employee();
         $employee->doSomeStuff();
         $employee->doMoreStuffThatCouldThrowExceptions();
    }
    catch(Exception $e) {
        handle_employee_exception($e);
    }
    

    It doesn’t get rid of the try/catch block in every file, but it does mean that you don’t have to duplicate the implementation of the exception-handling all the time. And don’t define handle_employee_exception as an instance method of the class, do it as a separate function, otherwise it will cause a fatal error if the exception is thrown in the constructor because the variable won’t exist.

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

Sidebar

Related Questions

I'm trying to understand the best options for pulling off a wizard form in
Been trying my best to understand this correctly. What is the difference between an
I'm fairly new to C programming but trying my best to understand it. I
My question is a bit tricky, I am trying best to make you understand
I am trying understand ViewModels deeper and I have read many articles and blogs
I'm trying to understand the best way to create line of business applications in
I am trying to understand conceptually the best way to deliver real streaming audio
I am trying to understand how to best make use of blocks in my
I´m new in python and I´m trying to find the best way to approach
I'm trying to understand best practices for unobtrusive Javascript. Among the ideas of unobtrusive

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.