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

StackOverflow

StackOverflow Logo StackOverflow Logo

StackOverflow Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Add group
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 564
Next

StackOverflow Latest Questions

Saralyn
  • 0
  • 0
SaralynBegginer
Asked: February 24, 20252025-02-24T22:44:15+00:00 2025-02-24T22:44:15+00:00In: PHP

exception – PHP Handle errors inside a function

  • 0
  • 0
exception – PHP Handle errors inside a function

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

The community is reviewing whether to reopen this question as of 4 days ago.

I have a function that tests whether the image received in parameters meets a certain number of criteria (file exists, it is not too large, what type, what compression, etc.).
I would like to set up a try catch block to find out if something goes wrong and to be able to receive a log. Here is the code in function.php called from main.php with include("function.php"):

function check_image($imagePath, $fileType, $targetFile, $imageSize, $courseImage){
$imageStatus=1;
$errors="";
try {
    //throw an exception here
    $compressionLevel="";
    $compressionLevel = ceil($compressionLevel);
} catch (\Throwable $th) {
    $errors.="Some error";
    $imageStatus =0;
}
return($imagestatus, $errors)
}

And from the main file main.php I call the function like that:

$result = check_image($imagePath, $fileType, $targetFile, $imageSize, $courseImage);            
$imgStatus = $result[0];
$errors = $result[1];

The problem is that if I deliberately trigger an exception (division by zero, type error or other) in the function, the execution stops but nothing is returned. The try catch block does not seem to capture the error.
I read here, for example, that if I implement set_error_handler it should give me something like this:

// Set Error Handler
set_error_handler (
    function($errno, $errstr, $errfile, $errline) {
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);     
    }
);
// Trigger an exception in a try block
try {
    $a = 3/0;
    echo $a;
}
catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
}

but this also doesn’t seem to give me something back from the function. I want to be able to return the error as parameter of the function. Any idea how to do this? Thanks

PS: The code was actually working fine and sent back an error in the log with:

      //faulty code below
      try{
        $compressionLevel="";
        $compressionLevel = ceil($compressionLevel);
      }catch (\Throwable $e) { // For PHP 7
        $errors.="Throwable error: ".$e->getMessage();
      // handle $e
    } catch (\Exception $e) { // For PHP 5
      // handle $e
      $errors.="Exception error";
    }
return array($imageStatus,$errors);

But I forgot to output the result in the main.php file like this:
main.php:

$result = check_image($imagePath, $fileType, $targetFile, $imageSize, $courseImage);
$imgStatus = $result[0];
$errors = $result[1];
console_log("What is been sent back from the function -"."\n". $imgStatus . "\n". $errors);

Here is the working result:

Localhost

Thanks to ADyson who understood the question correctly:
the following code provided is accurate:

0
  • 0 0 Answers
  • 67 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • W3spoint99

    What is Physics? Definition, History, Importance, Scope (Class 11)

    • 1 Answer
  • W3spoint99

    The Living World – Introduction, Classification, Characteristics, FAQs (Class 11 ...

    • 1 Answer
  • W3spoint99

    Explain - Biological Classification (Class 11 - Biology)

    • 1 Answer
  • Saralyn
    Saralyn added an answer When Humans look at their childhood pictures, the first thing… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Previously, length was measured using units such as the length… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Measurement forms the fundamental principle to various other branches of… January 17, 2025 at 3:25 pm

Related Questions

  • Reading fancy apostrophe PHP [duplicate]

    • 0 Answers
  • Unable to send mail via PHPMailer [SMTP->Error: Password not accepted ...

    • 0 Answers
  • Concerns when migrating from PHP 5.6 to 8.4 [closed]

    • 0 Answers
  • Laravel Auth::attempt() error: "Unknown column 'password'" when using a custom ...

    • 0 Answers
  • Core PHP cURL - header origin pass null value

    • 0 Answers

Trending Tags

biology class 11 forces how physics relates to other sciences interdisciplinary science learn mathematics math sets tutorial null sets physics physics and astronomy physics and biology physics and chemistry physics applications science science connections science education sets in mathematics set theory basics types of sets types of sets explained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

  • About US
  • Privacy Policy
  • Questions
  • Recent Questions
  • Web Stories

© 2025 WikiQuora.Com. All Rights Reserved

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.