Avatar of rgb192
rgb192Flag for United States of America asked on

I want to test the try catch by catching an error

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Using SplFileObject</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 8.8 - write_to_file3.php
// This page uses the SplFileObject class.

// Start the try...catch block:
try {
    
    // Create the object:
    $fp = new SplFileObject('data.txt', 'w');
    
    // Write the data:
    $fp->fwrite("This is a line of data1.\n");
    
    // Delete the object:
    unset($fp);

    // If we got this far, everything worked!
    echo '<p>The data has been written.</p>';

} catch (Exception $e) {
    echo '<p>The process could not be completed because the script: ' . $e->getMessage() . '</p>';
}

echo '<p>This is the end of the script.</p>';

?>
</body>
</html>

Open in new window



using wamp, how can I create an error naturally without adding an error creation line such as
error_log("You messed up!", 3, "my-errors.log");
trigger_error('Test Error', E_USER_ERROR);
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Gary

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
rgb192

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Using SplFileObject</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 8.8 - write_to_file3.php
// This page uses the SplFileObject class.
throw new Exception('Opps');
// Start the try...catch block:
try {
    
    // Create the object:
    $fp = new SplFileObject('data.txt', 'w');
    
    // Write the data:
    $fp->fwrite("This is a line of data1.\n");
    
    // Delete the object:
    unset($fp);

    // If we got this far, everything worked!
    echo '<p>The data has been written.</p>';

} catch (Exception $e) {
    echo '<p>The process could not be completed because the script: ' . $e->getMessage() . '</p>';
}

echo '<p>This is the end of the script.</p>';

?>
</body>
</html>

Open in new window



I added
throw new Exception('Opps'); to line 11

instead of
  echo '<p>The process could not be completed because the script: ' . $e->getMessage() . '</p>';

Fatal error: Uncaught exception 'Exception' with message 'Opps' in C:\wamp\www\phpvqp3_scripts\ch08\write_to_file3.php:11 Stack trace: #0 {main} thrown in C:\wamp\www\phpvqp3_scripts\ch08\write_to_file3.php on line 11



I want to run line 22 in the
catch (Exception $e) {}
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
rgb192

On your original example, simply create a data.txt file and make it read-only.
This will make this fail: $fp = new SplFileObject('data.txt', 'w');

when data.txt is 000 exception is still not caught



Rays example caught an exception
Exception Zero: Hello I Have Caught Exception Number Zero object(Exception_Zero)#2

but I think that example was made to intentionally catch. Not catch in a result of an error.




I am doing tutorials writing try/catch
catch in a result of an error but I do not know of an example of an error

It would seem to me that data.txt permissions would be an error.  Should I try on another server.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
rgb192

You need to put the throw in the try block else it won't be captured ;o)

Or you can wait until something fails, and if you're lucky the failure will not trigger an error, but will instead throw an exception.  In either case, if you've written the catch block(s) correctly, the exception object will be passed to your handler.


I do not understand try catch Originally I thought
try{
}catch{}
was
success{
}if error{}

but there is no way to call
if error{}

so now there is a way to give a 'fake error' which will never occur
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
rgb192

thanks for throwing try catch

An example of how error could be caught is database connection I think.
Ray Paseur

Yes, a data base connection error would be something that could be caught.  This article shows how a PDO connection can be wrapped in a try / catch block.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html