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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:41:16+00:00 2026-06-02T02:41:16+00:00

This is a piece of code that I was wondering if it should be

  • 0

This is a piece of code that I was wondering if it should be refactored to make it more complied with the Clean Code practices.

This is a class which is responsible for refunding some orders made by customers.

class RefundServiceInvoker {

    private $_orders;

    public function refundOrder() {   
        $this->getOrdersFromDB(); //This function gets all orders from DB and sets $_orders
        foreach ($this->_orders as $order) { 
            try {
                $order->refund(); //Some lines may throw an exception when refunded due to some business logic (ex. the order was already shipped)
                $this->updateOrderStatus('refunded')            
            } catch (Exception $e) {                   
                $this->logError($e);
                $this->sendMailToAdmin();
            }
        }
    }
}

of course this code is highly simplified than my original code.

My main problem is if $order->refund(); throws an exception it will be caught and logged to DB then a mail is sent. However what if $this->logError($e); itself throws an exception? or what if the mail server was down and an exception was thrown?

Better what if the DB was down itself and $this->getOrdersFromDB(); throws an exception?

My first solution was to wrap everything in one big try{}catch{}:

public function refundOrder() {   
            try {              
            $this->getOrdersFromDB(); //This function gets all orders from DB and sets $_orders
            foreach ($this->_orders as $order) { 

                    $order->refund(); //Some lines may throw an exception when refunded due to some business logic (ex. the order was already shipped)
                    $this->updateOrderStatus('refunded')            
                } catch (Exception $e) {                   
                    $this->logError($e);
                    $this->sendMailToAdmin();
                }
            }
        }

But that would mean if one order fails then all fails!! Should I put 2 try{}catch{} one for the whole function and the other for each order? But in this case too the functions in the catch might throw an exception that won’t be caught.

Note:

The application is built using Zend framework 1.11.11.

Thanks in advance.

  • 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-02T02:41:17+00:00Added an answer on June 2, 2026 at 2:41 am

    If you need to log and mail every exception, then you need something like this:

    class RefundServiceInvoker {
    
       private $_orders;
    
       public function refundOrder() {
          try {
             $this->getOrdersFromDB();
             foreach ($this->_orders as $order) {
                try {
                   $order->refund();
                } catch (MyBusinessException $e) {
                   # deals with the problematic $order without stopping the loop
                   $this->logError($e);
                   $this->sendMailToAdmin();
                }
                $this->updateOrderStatus('refunded');
             }
          } catch(Exception $e) {
             # deals with unexpected bugs
             $this->logError($e);
             $this->sendMailToAdmin();
          }
       }
    }
    

    But you have to put a try/catch inside log and mail methods to prevent them to throw any exception when servers are offline, for example. If you don’t, the $orders loop will be stopped when log/mail fails.

    If you need to log/mail only your business exception in refund() method, then you need something like this:

    class RefundServiceInvoker {
    
       private $_orders;
    
       public function refundOrder() {
          $this->getOrdersFromDB();
          foreach ($this->_orders as $order) {
             try {
                $order->refund();
             } catch (MyBusinessException $e) {
                # deals with the problematic $order without stopping the loop
                $this->logError($e);
                $this->sendMailToAdmin();
             }
             $this->updateOrderStatus('refunded');
          }
       }
    }
    

    Any other exception will lead to an http 500 – internal server error page, which is usually what you want, because it’s an unexpected bug.

    There’re other ways to handle this, but as you see, it depends on your needs.

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

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
I wrote this piece of code that is supposed to redirect something written on
I have this small piece of code that basically takes a list and runs
I've got this really simple piece of code that I thought was the correct
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that the ARC converter turned into this... //
I've been here and accordingly found out that this piece of code here var

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.