...

View Full Version : try



mikemrm
09-02-2009, 02:51 AM
Ok so I want to know if there is a way to try something like in python there is

try:
some code
except: pass

and basically if the code fails it will skip the rest of the code is there a way to do that in php like i know there is a try{} but i dont see anything that would have an except thing or is there another way of doing this
ex: include("OMG.txt"); but OMG.txt doesn't exist so instead of showing an error like

Warning: include(OMG.txt) [function.include]: failed to open stream: No such file or directory in GOD:\OMGNOWAY\really\why_are_u_reading_this\lolz.php on line ##

seco
09-02-2009, 03:14 AM
if
else
elseif

http://us.php.net/manual/en/control-structures.if.php



$filename = 'foo.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

mikemrm
09-02-2009, 03:18 AM
wow i totally forgot about that thanks alot

Fou-Lu
09-02-2009, 03:23 AM
Warning is an error, not an exception. In order to use a exception handling you need to catch something that is throwable:


function mySqrt($i)
{
if (!is_int($i) || $i < 0)
{
throw new InvalidArgumentException('Argument must be a positive integer!');
}
return sqrt($i);
}

try
{
mySqrt(-1);
}
catch (InvalidArgumentException $ex)
{
printf("Exception: %s\n", $ex->getMessage());
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum