Search the Community
Showing results for tags 'exceptions'.
-
Hi All, I have touched upon exceptions earlier. However I am still not sure if I am handling them correctly. try { ... ... ... }catch(Exception $e){ if($prod === true) // In production mode { header("Location: exceptions/errors.php") exit(); } if($dev === true) // In development mode { echo $e->getMessage(); // & if needed log the errors / exceptions into a file. exit(); } } I would like to ask if using the function header() to load the errors.php page is a good and safe practice. Or is there a better way to load the errors.php. If I load the errors page as in the snippet, do I also have to log the errors myself in some files or is php going to do that in any case. Any improvements or suggestions are welcome. Thanks all ! P.S. Googling exceptions gives loads of information but seldom does it touch the issue of loading an errors page when an exception occurs.
- 19 replies
-
- exceptions
- errors
-
(and 1 more)
Tagged with:
-
Hi all ! I have been using this snippet below to study exceptions: <?php mysqli_report(MYSQLI_REPORT_STRICT); try { $connection = new mysqli('localhos', 'root', '', 'test') ; } catch (Exception $e ) { echo "Server Error"; } ?> If, as in the snippet, I misspell the localhost, I am unable to override the php's own message. If, on the other hand I just misspell say the database name with the host spelt correctly, it overrides the php's default message. ( Default messages in both examples above being different). with the host name misspelt I receive the following warning: With the host name correct but a wrong database name I get the message defined in the snippet. Namely "Server Error". Kindly help resolve this. Thanks very much.
-
Not strictly php, but this seems a reasonable place to ask If documenting a routine ( which may or not directly throw an exception ), but whichh calls another one which definitely DOES, how would one write the @throws lines in the procedure header? 1. Ignore the exceptions thrown by called routines. 2. Attempt to create and maintain a list of @throws which reflect ANYTHING the routine and its callees can throw. 3. List exceptions thrown by this routine explicitly, and add @throws as fred() for each routine we know is called and may throw something. 1. Is consistent and easy to maintain, but scarcely useful. 2. Is a maintenance nightmare. 3. Is a slightly smaller nightmare. I guess what is needed is an automated system that scans the routine source but I haven't been introduced to anything of that ilk. David
-
I'm trying to figure out what the point in using try exceptions is. I recently learned about then and up until now, I've just used if statements. For example, lets say I wanna save an uploaded file to the server. Heres the code I use: if (move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"])) { echo "File saved. "</p>"; } else { echo "The file could not be saved"; } instead I could use: try { move_uploaded_file($_FILES["file"]["tmp_name"], "uploads//" . $_FILES["file"]["name"])) { } catch (Exception $e) { throw new Exception( 'File could not be uploaded', 0, $e); } but is there any point? Does exception handling give me any advantage over regular if statements? Also, I don't know how that $e variable works, does it store the type of error that occured or something? If so, would it store any info in this case? i.e. would it tell me why the file couldn't be saved?