|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-04-06 18:43 UTC] perching_eagle at yahoo dot com
Description:
------------
the compiler ought to bypass exceptions in "try" blocks, and allow a "catch block" to catch the exception at runtime. in other words, "try" blocks should turn compile-time errors to run-time errors. try blocks shouldn't depend on the throw keyword before throwing exceptions, errors in try blocks should automatically cause exceptions to be thrown. otherwise the current Exception class is only as good as this statement
"if(class_exists(Book)){//main code} else{//warning code}" for the code in my example.
Reproduce code:
---------------
<?php
try{
$err=new Book(); //class Book does not exist
//more code
}
catch(Exception $e){
print "class does not exist";
exit();
// or throw another exception that ends the program
//in another block.
}
Expected result:
----------------
output:(should look like this)
class does not exist
(python and java behave like this, i hope there will be some
consistence in logic, among open source languages )
Actual result:
--------------
program does not compile,
error message: Fatal error (actually says Fatel error),
class 'Book' not found on C:/XXXX/XXX
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 15:00:01 2025 UTC |
<?php $e="there is an error"; try{ throw new Exception ($e);//the throw can exist outside the try //block (or simply, almost anywhere) } try{ $err=34/0;//the compiler should ignore this error //it is legal because it is in a try block } $error=34/0;//the compiler should act on this error ?> //this error is illegal however zend engine will flag the error in the second try block (an abnormal behavior) instead of ignoring it. note: when flagging other posters comments as bogus, give logical reasons for doing so.well the logic behind exceptions is to help you handle ALL types of errors and i will show you a similar example in python. python code: num=input('enter a number, do not enter zero:) try: print 34/int(num) except ZeroDivisionError: print "error,you entered zero" (if you enter 2) output: 17 (if you enter 0) output: error, you entered zero #java does exactly the same thing. simple code, i didn't have to explicitly throw any exception (using the throw keyword) or include an "if" statement. this obeys the rule of keep things simple, and the "try" keyword actually does something, as opposed to being a mere decoration as it is now in PHP.correction= for the first line of the python code in the post before this one. # add qoutes to the parameter in the function 'input()' num=input("enter a number, do not enter zero:") the c programming language and other procedural languages can catch errors just like object oriented laguages that use exceptions, if the "try" keyword cannot force the compiler to overlook errors, then you have to use an "if" statement, just like in "c" and actually write more code. but if the "try" can suppress errors, you don't have to include "if" statements and the "throw" keyword, you just write "catch" statements that can catch each exception and error at runtime, and then you can continue you code without stopping it or allow the program to stop after sending a customized message on your site, rather than have your site or program crash. you just explicitly throw an exception that closes the program, after sending a customized message for fatal errors simple. ********************************************************************** to iliaa@php.net, a zero division error is an exception, it is not a fatal error, fatal errors require that you reset the program.they are errors that should not be caught such as linkage errors and thread death errors and some machine errors. the program should be allowed to end gracefully but could still catch them if you wish.