I am looking at the implementation of \Magento\Sales\Api\OrderRepositoryInterface on https://github.com/magento/magento2/blob/eacac63d35b97961e3304dc007cd6b78519a93d0/app/code/Magento/Sales/Model/OrderRepository.php

According to the code, the repository will throw NoSuchEntityException if the order does not exist. However, I figure it might be expensive overtime to rely on a try-catch statement if my function gets called quite often.

Is there a command/function that I can use to just get true/false on whether an order exist by given Order ID?

Thank you,

  • 1
    Why do you think it's expensive to rely on try-catch ?
    – Marius
    Commented Sep 30, 2016 at 13:36
  • Is it not? I was on the assumption that it is. In term of scalability, would it be ok to use try and catch for NoSuchEntityException to create an order if it is not exist?
    – frostshoxx
    Commented Sep 30, 2016 at 13:48
  • 3
    I may be wrong here, but I don't think there is a problem. You can do a simple test and profile it.
    – Marius
    Commented Sep 30, 2016 at 13:52

1 Answer 1


There's some discussion of the overhead of exceptions in PHP here: https://stackoverflow.com/questions/104329/performance-of-try-catch-in-php

But more to the point, the overhead of exceptions (which is very small to begin with) would far and away be dwarfed by the overhead of actually checking whether the order in question exists (loading the ID in question from the database).

If your function gets called 'quite often', it might make sense to consider alternate approaches that would involve less loading from the database (depending on circumstances). Whether or not you catch exceptions isn't going to make a notable difference though.

Use exceptions the way Magento intends. They won't kill you. Be careful about trying to over-optimize the wrong things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.