I am just not understanding how this throw exception stuff works. I tried to set up a couple errorhandlers that were included in the file.
When I run the following code I don't get the message shown on the throw Exception line... instead I get the message from the catch line. How do I get my new exception here to report the error message the way I intended it to print out?
Here is the code snippet:
try
{
@$db = new mysqli("localhost", "omgma_omgmauser", $pword, "omgma_members");
if (mysqli_connect_errno() > 0)
{
throw new Exception("This is the throw exception on line 72: db_connect cound not connect to database:\n".mysqli_connect_error());
}
else
{
echo "Connection worked! <br>";
return $db;
}
}
catch (Exception $e){
echo "Program Exception caught: <br />Error message: $e";}
When I run that what I see on my browser screen is:
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)
Program Exception caught:
Error message: Object id #2
My error handler code I am trying to use is as follows:
<?php
function on_error($num, $str, $file, $line)
{
print "<p><strong>Encountered error: </strong>$num in $file, line $line: $str\n</p>";
}
/*set_error_handler("on_error");
print $foo;
restore_error_handler( );
print $foo;*/
function handle_exception($exception)
{
echo "<p><strong>Problem: </strong>{$exception->getMessage()}\n</p>";
}
/*set_exception_handler("handle_exception");
throw(new Exception('this is a test exception'));*/
// Create a handler function for assert
function assert_failed($file, $line, $code)
{
echo "<hr>Assertion Failed! <br />
File: '$file'<br />
Line: '$line'<br />
Code: '$code'<br /><hr />";
}
//set handlers for debugging:
set_error_handler("on_error");
set_exception_handler("handle_exception");
assert_options(ASSERT_CALLBACK, 'assert_failed');
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 1);
?>Hey,
Your nearly right.
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)
Program Exception caught:
Error message: Object id #2
"Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)"
This part is from @$db = new mysqli("localhost", "omgma_omgmauser", $pword, "omgma_members"); no doubt.
The @ symbol should be beside @mysqli - Not @$db.
Use $e->getMessage() to retrieve the actual message you threw, as per <!-- m --><a class="postlink" href="http://au3.php.net/exceptionsThe">http://au3.php.net/exceptionsThe</a><!-- m --> whole point of using exceptions is that you shouldn't have to write any error checking code in most cases.
Remove the @, and have an error handler which converts errors to exceptions. Then it will automatically throw for you with no extra code involved.
Mark
When I run the following code I don't get the message shown on the throw Exception line... instead I get the message from the catch line. How do I get my new exception here to report the error message the way I intended it to print out?
Here is the code snippet:
try
{
@$db = new mysqli("localhost", "omgma_omgmauser", $pword, "omgma_members");
if (mysqli_connect_errno() > 0)
{
throw new Exception("This is the throw exception on line 72: db_connect cound not connect to database:\n".mysqli_connect_error());
}
else
{
echo "Connection worked! <br>";
return $db;
}
}
catch (Exception $e){
echo "Program Exception caught: <br />Error message: $e";}
When I run that what I see on my browser screen is:
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)
Program Exception caught:
Error message: Object id #2
My error handler code I am trying to use is as follows:
<?php
function on_error($num, $str, $file, $line)
{
print "<p><strong>Encountered error: </strong>$num in $file, line $line: $str\n</p>";
}
/*set_error_handler("on_error");
print $foo;
restore_error_handler( );
print $foo;*/
function handle_exception($exception)
{
echo "<p><strong>Problem: </strong>{$exception->getMessage()}\n</p>";
}
/*set_exception_handler("handle_exception");
throw(new Exception('this is a test exception'));*/
// Create a handler function for assert
function assert_failed($file, $line, $code)
{
echo "<hr>Assertion Failed! <br />
File: '$file'<br />
Line: '$line'<br />
Code: '$code'<br /><hr />";
}
//set handlers for debugging:
set_error_handler("on_error");
set_exception_handler("handle_exception");
assert_options(ASSERT_CALLBACK, 'assert_failed');
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 1);
?>Hey,
Your nearly right.
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)
Program Exception caught:
Error message: Object id #2
"Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 78: mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)"
This part is from @$db = new mysqli("localhost", "omgma_omgmauser", $pword, "omgma_members"); no doubt.
The @ symbol should be beside @mysqli - Not @$db.
Use $e->getMessage() to retrieve the actual message you threw, as per <!-- m --><a class="postlink" href="http://au3.php.net/exceptionsThe">http://au3.php.net/exceptionsThe</a><!-- m --> whole point of using exceptions is that you shouldn't have to write any error checking code in most cases.
Remove the @, and have an error handler which converts errors to exceptions. Then it will automatically throw for you with no extra code involved.
Mark