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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:58:00+00:00 2026-05-21T05:58:00+00:00

for the first time i came across try, throw catch statement in PHP, and

  • 0

for the first time i came across try, throw catch statement in PHP, and i felt this could be the better way for handling errors as i was quite messing my error handlers with lots of if else statements, however as i am performing CRUD operations on my script i wanted my error handlers to perform two task.

  1. display the user readable or
    custom error message back to the
    user.
  2. catch all the error in a file for
    me to read.

i am using the following code..

try
{
    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    if($cname == $name)
    {
        throw new Exception('Sorry, Please Change the value to update ');
    }
    $sth = $dbh->prepare("UPDATE countries SET name = :name WHERE id = :cid");
    $sth->bindParam(':name', $name);
    $sth->bindParam(':cid', $cid);
    $sth->execute();
}
catch(PDOException $e)
{
    echo $e->getMessage();
    file_put_contents("resources/logs/Connection-log.txt", DATE.PHP_EOL.$e->getMessage().PHP_EOL.PHP_EOL, FILE_APPEND);
}

if the condition $cname == $name is true i just want to display the error ‘Sorry, Please Change the value to update, however this is not happening here, instead it throws the Fatal Error with this message.

Fatal error: Uncaught exception 'Exception' with message 'Sorry, Please Change the value to update ' in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/countries.php:24 Stack trace: #0 /Applications/MAMP/htdocs/kokaris/administrator/location-manager.php(43): include() #1 {main} thrown in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/countries.php on line 24

how do i achieve this?

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-05-21T05:58:01+00:00Added an answer on May 21, 2026 at 5:58 am

    Your catch is catching a PDOException :

    catch(PDOException $e)
    

    While you are throwing a Exception :

    throw new Exception('Sorry, P...
    

    PDOException is a sub-class of Exception, which means that :

    • A PDOException is an Exception
    • But an Exception is not a PDOException.

    So, when you are trying to catch a PDOException, your catch will not also catch Exception.

    If you want your Exception to be catched, you must use something like this :

    try {
    
    } 
    catch (PDOException $e) {
      // deal with PDOException
    }
    catch (Exception $e) {
      // deal with all other kinds of exceptions
    }
    

    In this case, the catch PDOException can be avoided, if you do not want to do some special treatment for PDOExceptions, and just want all exceptions to be dealt with the same way :

    try {
    
    } 
    catch (Exception $e) {
      // deal with all kinds of exceptions
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the Python with statement for the first time today. I've been
This is the first time I've came across this. Just printed a list and
I came across Side-by-side Assemblies for the first time today while trying to install
The first time I try to open a particular form many of the controls
This is my first time attempting to call an ASP.NET page method from jQuery.
This is my first time using joomla. I don't know if I'm using the
This is my first time with Web services. I have to develop web services
First of all, it's great. However, I came across a situation where my benchmarks
I am learning Python and came across this example: W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6)) b =
first time SO user :) I know that I can format a number like

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.