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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:53:09+00:00 2026-06-10T18:53:09+00:00

Regarding function calls, is it better to give the caller the responsibility of checking

  • 0

Regarding function calls, is it better to give the caller the responsibility of checking if a function worked or failed, or should the error be announced by the function itself?

The reason I’m asking is because I don’t like mixing presentation with logic, however if the caller has to error-check, it can be inaccurate and cumbersome eg.

if(!login($username, $password)
{
    echo 'Login failed. Please try again.';
}

The caller has no idea why it failed. Was the username/password combo wrong, or was it a database connection failure? Or some other unexpected reason?

This would not be a problem if we can error check/throw exceptions inside the functions, because then we would have specific errors at specific points of code eg database connection failures…but that would be mixing presentation and logic. What is the best practice here?

Thank you.

  • 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-10T18:53:11+00:00Added an answer on June 10, 2026 at 6:53 pm

    I would personally give the caller the responsibility by throwing an exception in the business logic (model) layer. Then, the exception can be caught in the controller (and the error assigned a variable in the view). Some people even like to catch exceptions directly in the presentation layer, but this does not apply well in the case of web development. As long as the exception is only used for presentation purposes in the view, I don’t consider it much of a problem, though.

    So, I would not do as you did in your simple example, because the caller may not always want an error to be displayed. By handling the responsibility to the caller, he or she has a choice. Also, I don’t like echoing content in the business logic (I prefer to throw exceptions and keep the models clean from presentation), but your code was most likely just an oversimplified example.

    Edit: You could do something like this:

    Model:

    function login($username, $password) {
        if (login failed) {
            throw new Login_Exception();
        }
    
        else {
            // Set session
            return true;
        }
    }
    

    Controller:

    try {
        $model->login($username, $password);
    }
    
    catch (Login_Exception $e) {
        $view->loginError = 'There was an error logging in.';
    }
    

    You can then throw different exceptions to indicate exactly what went wrong (e.g. Wrong_Username_Exception) and handle it accordingly. Alternatively you could even provide a user friendly reason in the exception’s constructor, but in my opinion that would be coupling presentation and logic a little too much.

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

Sidebar

Related Questions

I have a doubt regarding JNI function calls made from Android. Do they run
This question is regarding getopt function in php. I need to pass two parameter
I have a question regarding static function in php. let's assume that I have
Regarding global variable initialization, function hello_testing() { global $conditional_random; if (isset($conditional_random)) { echo foo
I have question regarding the use of function parameters. In the past I have
I have a problem regarding the datediff MYSQL function, I can use it and
Regarding the question on SO How to overload the calling process of any function
I have a query regarding the directory returned from Path.GetTempPath() function. It returns C:\Documents
I'm about to use func_get_args for reading additional arguments of a function call. How
I need to call the co-class function by reading its address from vtable of

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.