How to throw custom error in PHP mysql - php

$row=mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error())
In this code the mysql_error will be displayed if mysql_fetch_array fails. Is there any way to throw that mysql_error to reload a page instead.
P.S.: I know mysql has been depreciated. Its an old project so please don't suggest in your answers to use mysqli and PDO. I am well aware of it. Please tell me the way to throw reload page as error.

You could use try catch for exception handling in PHP
http://www.php.net/manual/en/language.exceptions.php
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something wrong', 0, $e);
}
OR
try
{
// do something that can go wrong
}
catch (Exception $e)
{
if ($e instanceof CustomException)
{
// do something
}
else if ($e instanceof OtherException)
{
// do something
}
else
{
// do something
}
}
Or
try
{
}
catch (CustomException $e)
{
}
catch (OtherException $e)
{
}
catch (Exception $e)
{
}

Related

PHP exception inside catch: how to handle it?

Suppose to have a PHP code inside a try...catch block. Suppose that inside catch you would like to do something (i.e. sending email) that could potentially fail and throw a new exception.
try {
// something bad happens
throw new Exception('Exception 1');
}
catch(Exception $e) {
// something bad happens also here
throw new Exception('Exception 2');
}
What is the correct (best) way to handle exceptions inside catch block?
Based on this answer, it seems to be perfectly valid to nest try/catch blocks, like this:
try {
// Dangerous operation
} catch (Exception $e) {
try {
// Send notification email of failure
} catch (Exception $e) {
// Ouch, email failed too
}
}
You should not throw anything in catch. If you do so, than you can omit this inner layer of try-catch and catch exception in outer layer of try-catch and process that exception there.
for example:
try {
function(){
try {
function(){
try {
function (){}
} catch {
throw new Exception("newInner");
}
}
} catch {
throw new Exception("new");
}
}
} catch (Exception $e) {
echo $e;
}
can be replaced to
try {
function(){
function(){
function (){
throw new Exception("newInner");
}
}
}
} catch (Exception $e) {
echo $e;
}
You have 2 possible ways:
You exit the program (if it is severe) and you write it to a log file and inform the user.
If the error is specifically from your current class/function,
you throw another error, inside the catch block.
You can use finally. Code in this branch will be executed even if exception is thrown within catch branch

PHP: is it possible to resume the try block after the exception is caught?

Let's say I have codes like this:
try{
doTaskA();
doTaskB();
doTaskC();
} catch(MyException $e){
fixMyException();
}
When doTaskA() throws MyException, I want the program to go through fixMyException(), then it goes back and continues executing doTaskB() and doTaskC(). Is it possible?
The same thing should apply to other tasks, i.e. all doTaskA/B/C() may throw the same exception, and I want the program can go on to the unfinished tasks every time after it executes fixMyException()..
Is there any thing like "continue" or "resume" function in the exception handling?
I have checked the PHP manual but it seems such control structure doesn't exist. If so, what's the best practice to design the program that can do what I want? (Supposed I have over 10 taskes inside the try block).
function do_all_tasks($position=0)
{
$tasks = array('doTaskA', 'doTaskB', 'doTaskC', ...);
$size = count($tasks);
for ($i=$position; $i<$size; ++$i)
{
try
{
$func = $tasks[$i];
$func();
}
catch (Exception $e)
{
fixMyException();
do_all_tasks($i+1);
};
}
}
do_all_tasks();
try{
doTaskA();
} catch(MyException $e){
fixMyException();
}
try{
doTaskB();
} catch(MyException $e){
fixMyException();
}
try{
doTaskC();
} catch(MyException $e){
fixMyException();
}
How is that any different than doing
try {
doTaskA();
} catch (MyException $e) {
fixMyException();
}
try {
doTaskB();
} catch (MyException $e) {
fixMyException();
}
try {
doTaskC();
} catch (MyException $e) {
fixMyException();
}
No it is not possible. You'd need to put each of doTask*() functions in it's own separate try-catch block.

Try-Catch all Exceptions with Zend_Http_Client?

I have the following function:
public function getClientTable($feedUrl)
{
$client = new Zend_Http_Client($feedUrl);
try
{
return $client->request()->getBody();
}
catch (Zend_Http_Client_Adapter_Exception $e)
{
return false;
}
}
It seems to work great for catching that specific Zend_Http_Client_Adapter_Exception; but what if I want it to catch additional exceptions? Hell, what if I wanted it to catch ALL exceptions... how would I do this?
Also, should I be using "return" or "throw" in the try? Why does it matter?
You can have multiple catch statements, eg
try {
// whatever
} catch (Zend_Http_Client_Adapter_Exception $e) {
// ah ha
} catch (Zend_Some_other_Exception $e) {
// ah ha
} catch (Exception $e) {
// And the final fallback catch that grabs all exceptions
}

Rethrow php exception into higher level catch block

I am trying to pass an exception from a specific catch block to a more general catch block. However it does not appear to be working. I get a 500 server error when I try the following. Is this even possible?
I realize that there are easy workarounds, but isn't it normal to say, "Hey I don't feel like dealing with this error, let's have the more general exception handler take care of it!"
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
catch (Exception $e) {
echo $e->getMessage();
}
Technically this is what you're looking for:
try {
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
}
catch (Exception $e) {
echo $e->getMessage();
}
however I agree that exceptions shouldn't be used for flow control. A better way would be like this:
function show_error($message) {
echo "Error: $message\n";
}
try {
//some soap stuff
}
catch (SoapFault $sf) {
show_error('Soap Fault');
}
catch (Exception $e) {
show_error($e->getMessage());
}

php try ... else

Is there something similar in PHP to the try ... else in Python?
I need to know if the try block executed correctly as when the block executed correctly, a message will be printed.
PHP does not have try/catch/else. You could however set a variable in the catch block that can be used to determine if it was run:
$caught = false;
try {
// something
} catch (Exception $e) {
$caught = true;
}
if (!$caught) {
}
I think the "else" clause is a bit limiting, unless you don't care about any exceptions thrown there (or you want to bubble those exceptions)... From my understanding of Python, it's basically the equivalent of this:
try {
//...Do Some Stuff Here
try {
// Else block code here
} catch (Exception $e) {
$e->elseBlock = true;
throw $e;
}
} catch (Exception $e) {
if (isset($e->elseBlock) && $e->elseBlock) {
throw $e;
}
// catch block code here
}
So it's a bit more verbose (since you need to re-throw the exceptions), but it also bubbles up the stack the same as the else clause...
Edit Or, a bit cleaner version (5.3 only)
class ElseException extends Exception();
try {
//...Do Some Stuff Here
try {
// Else block code here
} catch (Exception $e) {
throw new ElseException('Else Clasuse Exception', 0, $e);
}
} catch (ElseException $e) {
throw $e->getPrevious();
} catch (Exception $e) {
// catch block code here
}
Edit 2
Re-reading your question, I think you may be overcomplicating things with an "else" block... If you're just printing (which isn't likely to throw an exception), you don't really need an else block:
try {
// Do Some stuff
print "Success";
} catch (Exception $e) {
//Handle error here
print "Error";
}
That code will only ever print either Success or Error... Never both (since if the print function throws the exception, it won't be actually printed... But I don't think the print CAN throw exceptions...).
Not familiar with python but it sounds like you're after Try Catch blocks used with exceptions...
http://php.net/manual/en/language.exceptions.php
There is try-catch in php.
Example:
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
else return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
try {
$clean = false;
...
$clean = true;
} catch (...) { ... }
if (!$clean) {
//...
}
That's the best you can do.
You can use try { } catch () { } and throw. See http://php.net/manual/en/language.exceptions.php
try {
$a = 13/0; // should throw exception
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
or manually:
try {
throw new Exception("I don't want to be tried!");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

Categories