Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :
<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) { - dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) { - dosomething();*
}
The continue keyword would resume the execution from where it had
diverged, according to the function which led to the SomeException
catch block.
So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.
Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.
Julien.Pauli
Sounds interesting. And what about the second attempt passing the try block
without goto?
2013/4/26 Julien Pauli jpauli@php.net
Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}The continue keyword would resume the execution from where it had
diverged, according to the function which led to the SomeException
catch block.So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.Julien.Pauli
2013/4/26 Julien Pauli jpauli@php.net
Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}
This seems like a BC break:
for ( ; ; ) {
try {
...
} catch (Exception $e) {
continue;
}
}
With the proposed syntax, "continue" will no longer refer to the "for" loop.
The continue keyword would resume the execution from where it had
diverged, according to the function which led to the SomeException
catch block.So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.Julien.Pauli
Lazare INEPOLOGLOU
Ingénieur Logiciel
Hi
2013/4/26 Lazare Inepologlou linepogl@gmail.com:
This seems like a BC break:
for ( ; ; ) {
try {
...
} catch (Exception $e) {
continue;
}
}With the proposed syntax, "continue" will no longer refer to the "for" loop.
Well, at the moment "continue" is assigned to only usage in loops, we
also have "break" (switch) which works for both loops and construct,
if break is used inside a construct, thats inside a loop, it will be
seen as a part of the construct, we could change continue to work the
same as that.
--
regards,
Kalle Sommer Nielsen
kalle@php.net
On Friday 26 April 2013 16:41:17 Julien Pauli wrote:
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}
...
So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.
What exactly would it "continue" with?
The next instruction after whereever the "throw" was?
The toplevel function call (or other code) next after the toplevel function in
the try block that threw up?
The first answer won't work because any object live when bar() threw but only
referenced in the call stack below the try block, will already have been
destructed.
The second answer appears ... semantically dubious.
best regards
Patrick
I will answer, as the idea came from Pierrick Charron and I :-)
Sometimes, you call functions (or methods) and you know they can throw some
exceptions. But you don't want to stop all computations just because one of
them raised an exception. Maybe you just want to log it, and go to the next
call.
If you write that:
try {
$o->method1();
$o->method2();
$o->method3();
} catch (Exception $e) {
add_to_log($e->getMessage());
}
If method1() raises an exception, it's not possible to execute method2().
And so it forces us to write ugly code like that:
try {
$o->method1();
} catch (Exception $e) {
add_to_log($e->getMessage());
}
try {
$o->method2();
} catch (Exception $e) {
add_to_log($e->getMessage());
}
try {
$o->method3();
} catch (Exception $e) {
add_to_log($e->getMessage());
}
But honestly, nobody uses a try/catch block for every single line of code!
Unfortunately, it's sometimes the only solution...
What I'm suggesting is to be able to continue the code execution, from to
point where an exception was raised.
try {
$o->method1();
$o->method2();
$o->method3();
} catch (Exception $e) {
add_to_log($e->getMessage());
continue;
}
In this example, if method1() raises an exception, it will be logged, and
then method2() will be called.
If method2() raises an exception, it will be logged, and then method3()
will be called.
Is method3() raises an exception, it will be logged, and the execution will
continue after the try/catch block.
"continue" is the best keyword for that: The meaning is "please, continue
the execution of my code" :-)
As Julien said, there is a BC break, when a try/catch block is written
inside a loop. But I think it's not a major usage, and it's a minor
inconvenient.
Amaury
2013/4/26 Patrick Schaaf php@bof.de
On Friday 26 April 2013 16:41:17 Julien Pauli wrote:
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}
...
So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.What exactly would it "continue" with?
The next instruction after whereever the "throw" was?
The toplevel function call (or other code) next after the toplevel
function in
the try block that threw up?The first answer won't work because any object live when bar() threw but
only
referenced in the call stack below the try block, will already have been
destructed.The second answer appears ... semantically dubious.
best regards
Patrick
As Julien said, there is a BC break, when a try/catch block is written
inside a loop. But I think it's not a major usage, and it's a minor
inconvenient.
Yeah, and catching and discarding exceptions also possible, albeit a minor
inconvinience.
I don't have a strong opinion on this feature(albeit I think it isn't a
coincidence that most languages doesn't offer a dedicated solution to this
problem) but please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.
2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.
Fair enough. We can use "resume".
try {
doThis();
doThat();
} catch (NotImportantException $nie) {
addToLog($nie->getMessage());
resume;
} catch (Exception $e) {
cleanupEverything();
}
Sorry but I disagree, I think you're approaching try-catch wrong.
You shouldn't have a try-catch that can continue on the next line after
the throw.
What you should do is have decoupled code that handles their own
exceptions nicely and either cleans up after itself else it rethrows the
exception/a new one.
Any top level try-catch is supposed to be a control structure with one of
two chances: either lines 1-N go smoothly and no exceptions are thrown, or
Exception_X is thrown and you clean up on it's catch block...
Also remember that you have the finally block.
2013/4/27 Daniel Macedo admacedo@gmail.com
Sorry but I disagree, I think you're approaching try-catch wrong.
You shouldn't have a try-catch that can continue on the next line after
the throw.
What you should do is have decoupled code that handles their own
exceptions nicely and either cleans up after itself else it rethrows the
exception/a new one.Any top level try-catch is supposed to be a control structure with one of
two chances: either lines 1-N go smoothly and no exceptions are thrown, or
Exception_X is thrown and you clean up on it's catch block...
It's an opinion, a software engineering choice. Not something that must be
enforced by the language.
It could be very useful in some situations, not all the time (as traits,
for example).
Ideally, every errors (in fact, any abnormal situation) should raise an
exception, right? OK, but that doesn't mean that any abnormal situation
should be able to break execution flow, with no other solution than writing
more code.
Also remember that you have the finally block.
Can you explain how the finally block can help in my example?
I agree with Amaury.
Although, it is rather smelly code to use try/catch as control flow
instrument, in some situations it is clearer to do it this way.
I think the new construct would be especially useful if one just wants to
log errors (no further exception handling is necessary) as shown in previous
examples.
-----Ursprüngliche Nachricht-----
Von: amaury.bouchard@gmail.com [mailto:amaury.bouchard@gmail.com] Im Auftrag
von Amaury Bouchard
Gesendet: Samstag, 27. April 2013 19:23
An: Daniel Macedo
Cc: Ferenc Kovacs; Julien Pauli; PHP Internals; Patrick Schaaf
Betreff: Re: [PHP-DEV] Continued try blocks
2013/4/27 Daniel Macedo admacedo@gmail.com
Sorry but I disagree, I think you're approaching try-catch wrong.
You shouldn't have a try-catch that can continue on the next line
after the throw.
What you should do is have decoupled code that handles their own
exceptions nicely and either cleans up after itself else it rethrows
the exception/a new one.Any top level try-catch is supposed to be a control structure with one
of two chances: either lines 1-N go smoothly and no exceptions are
thrown, or Exception_X is thrown and you clean up on it's catch block...
It's an opinion, a software engineering choice. Not something that must be
enforced by the language.
It could be very useful in some situations, not all the time (as traits, for
example).
Ideally, every errors (in fact, any abnormal situation) should raise an
exception, right? OK, but that doesn't mean that any abnormal situation
should be able to break execution flow, with no other solution than writing
more code.
Also remember that you have the finally block.
Can you explain how the finally block can help in my example?
I agree with Amaury.
Although, it is rather smelly code to use try/catch as control flow
instrument, in some situations it is clearer to do it this way.
I think the new construct would be especially useful if one just wants to
log errors (no further exception handling is necessary) as shown in previous
examples.
Logging is a different thing from error handling. exceptions are a tool
to handle (exceptional critically?) error situations.
johannes
2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.Fair enough. We can use "resume".
"continue" is just a keyword (syntactic sugar) we sure can change, I like
"resume" yes :-)
Julien.Pauli
2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.Fair enough. We can use "resume".
"continue" is just a keyword (syntactic sugar) we sure can change, I like
"resume" yes :-)Julien.Pauli
And how about a restart instead of resume? I have used try catch blocks as a type of transactional block, so I think it would be nice if I could restart the entire block instead of resuming from the last point where it failed:
$blue = 'blue';
try {
$data = a($blue);
b($data); // This throws the dataIntegrityException
c();
} catch (dataIntegrityException $e) {
$blue = 'is the new red';
restart; // executes a(), b() and c() again
} catch (Exception $e) {
rollback();
}
Greetings.
Why not. But it will come in addition to "resume", not instead of it.
Then:
- "resume" => continue execution just after the point where the exception
was raised. - "restart" => restart the whole try block.
I don't understand the meaning of your "rollback".
2013/4/29 Camilo Sperberg unreal4u@gmail.com
On Sat, Apr 27, 2013 at 3:56 PM, Amaury Bouchard amaury@amaury.net
wrote:2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a
loop
context.
For example you have a retry counter decremented in a loop and you
catch
the exceptions and retry until the retry limit is reached.Fair enough. We can use "resume".
"continue" is just a keyword (syntactic sugar) we sure can change, I like
"resume" yes :-)Julien.Pauli
And how about a restart instead of resume? I have used try catch blocks as
a type of transactional block, so I think it would be nice if I could
restart the entire block instead of resuming from the last point where it
failed:$blue = 'blue';
try {
$data = a($blue);
b($data); // This throws the dataIntegrityException
c();
} catch (dataIntegrityException $e) {
$blue = 'is the new red';
restart; // executes a(), b() and c() again
} catch (Exception $e) {
rollback();
}Greetings.
Why not. But it will come in addition to "resume", not instead of it.
Then:
- "resume" => continue execution just after the point where the exception was raised.
- "restart" => restart the whole try block.
I don't understand the meaning of your "rollback".
2013/4/29 Camilo Sperberg unreal4u@gmail.com
2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.Fair enough. We can use "resume".
"continue" is just a keyword (syntactic sugar) we sure can change, I like
"resume" yes :-)Julien.Pauli
And how about a restart instead of resume? I have used try catch blocks as a type of transactional block, so I think it would be nice if I could restart the entire block instead of resuming from the last point where it failed:
$blue = 'blue';
try {
$data = a($blue);
b($data); // This throws the dataIntegrityException
c();
} catch (dataIntegrityException $e) {
$blue = 'is the new red';
restart; // executes a(), b() and c() again
} catch (Exception $e) {
rollback();
}Greetings.
It was just to denote the nature of a transaction block, nothing important ;)
2013/4/28 Julien Pauli jpauli@php.net
On Sat, Apr 27, 2013 at 3:56 PM, Amaury Bouchard amaury@amaury.net
wrote:2013/4/27 Ferenc Kovacs tyra3l@gmail.com
please don't reuse the continue keyword for it.
There are a bunch of code out there where which uses exceptions in a
loop
context.
For example you have a retry counter decremented in a loop and you catch
the exceptions and retry until the retry limit is reached.Fair enough. We can use "resume".
"continue" is just a keyword (syntactic sugar) we sure can change, I like
"resume" yes :-)Julien.Pauli
Does this sound familiar to anyone?
Sub Foo( Bar )
On Error GoTo ErrorHandler
. . .
Exit Sub
ErrorHandler:
. . .
Resume Next
End Sub
:-)
Lazare INEPOLOGLOU
Ingénieur Logiciel
Am 26.04.13 16:41, schrieb Julien Pauli:
Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}The continue keyword would resume the execution from where it had
diverged, according to the function which led to the SomeException
catch block.So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.Julien.Pauli
Hi Julien.
What about the following example?
try {
$foo = $bar->getObject();
$foo->doSomething()
} catch(Exception $e) {
continue // Or whatever shall be used
}
When $bar->getObject throws an Exception no $foo is set. So the next
line will result in a "PHP Fatal error: Call to a member function
doSomething() on a non-object".
So where shall the "continue" actually continue? In the line after the
exception was thrown? In the line following the last line $foo was
accessed in?
Or would something like
CreateTryCatchBlockForEverySingleLine {
$bar->doOne();
$bar->doNext();
$bar->doSomethingElse();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
resulting in
try {
$bar->doOne();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
try {
$bar->doNext();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
try {
$bar->doSomethingElse();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
be more what you are talking about?
Regards
Andreas
--
,,,
(o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl |
| mailto:andreas@heigl.org N 50°22'59.5" E 08°23'58" |
| http://andreas.heigl.org http://hei.gl/wiFKy7 |
+---------------------------------------------------------------------+
| http://hei.gl/root-ca |
+---------------------------------------------------------------------+
I like the idea but would prefer another control structure such as
CreateTryCatchBlockForEverySingleLine (should be shorter though) rather than
continue.
The continue keyword is confusing in my opinion. And I would say it should
be for every single statement whereupon a statement is defined by either ;
or {} rather then for every single line.
Thus the following code:
tryStatements{
$a = foo();
if($a > 1){
$i = bar();
}else{
$i = muu();
}
for(;$i<10;$i++){
//..
}
}catch(Exception $ex){
log($ex);
}
Would result in:
try{
$a = foo();
} catch(Exception $ex){
log($ex);
}
try{
if($a > 1){
$i = bar();
}else{
$i = muu();
}
} catch(Exception $ex){
log($ex);
}
try{
for(;$i<10;$i++){
//..
}
} catch(Exception $ex){
log($ex);
}
-----Ursprüngliche Nachricht-----
Von: Andreas Heigl [mailto:andreas@heigl.org]
Gesendet: Freitag, 26. April 2013 20:15
An: Julien Pauli
Cc: PHP Internals
Betreff: Re: [PHP-DEV] Continued try blocks
Am 26.04.13 16:41, schrieb Julien Pauli:
Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}The continue keyword would resume the execution from where it had
diverged, according to the function which led to the SomeException
catch block.So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.Julien.Pauli
Hi Julien.
What about the following example?
try {
$foo = $bar->getObject();
$foo->doSomething()
} catch(Exception $e) {
continue // Or whatever shall be used
}
When $bar->getObject throws an Exception no $foo is set. So the next
line will result in a "PHP Fatal error: Call to a member function
doSomething() on a non-object".
So where shall the "continue" actually continue? In the line after the
exception was thrown? In the line following the last line $foo was
accessed in?
Or would something like
CreateTryCatchBlockForEverySingleLine {
$bar->doOne();
$bar->doNext();
$bar->doSomethingElse();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
resulting in
try {
$bar->doOne();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
try {
$bar->doNext();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
try {
$bar->doSomethingElse();
} catch(Exception $e) {
doSomethingWithTheException($e);
}
be more what you are talking about?
Regards
Andreas
--
,,,
(o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl |
| mailto:andreas@heigl.org N 50°22'59.5" E 08°23'58" |
| http://andreas.heigl.org http://hei.gl/wiFKy7 |
+---------------------------------------------------------------------+
| http://hei.gl/root-ca |
+---------------------------------------------------------------------+
2013/4/26 Andreas Heigl andreas@heigl.org
try {
$foo = $bar->getObject();
$foo->doSomething()
} catch(Exception $e) {
continue // Or whatever shall be used
}When $bar->getObject throws an Exception no $foo is set. So the next
line will result in a "PHP Fatal error: Call to a member function
doSomething() on a non-object".
That's fine to me.
It's a software engineering problem you can solve easily:
try {
$foo = $bar->getObject();
$foo->doSomething();
} catch (ObjectCreationException $oce) {
add_to_log('Unable to create object');
throw $oce; // or do something else,
// or just do nothing to exit from the try/catch block
} catch (Exception $e) {
add_to_log($e->getMessage());
continue;
}
The getObject() method should raise an ObjectCreationException, while the
doSomething() method could raise any other type of exception.
It's just a natural and smart way to use exceptions.
It could be solve differently:
try {
$foo = $bar->getObject();
$foo->doSomething();
} catch (NotImportantException $nie) {
add_to_log($nie->getMessage());
continue;
} catch (Exception $e) {
add_to_log($e->getMessage());
throw $e; // or do something else,
// or just do nothing to exit from the try/catch block
}
If an expression raises a NotImportantException, it will not interrupt the
execution flow.
Hi Julien,
I'm still on the fence, not sure if it isn’t too opaque.
Am 26.04.2013 um 16:41 schrieb Julien Pauli jpauli@php.net:
[...]
One question regarding scoping: does the next statement inherit the scope of the catch block like this?
try {
$user = $repository->findById(123);
$user->setName($name);
$em->save($user);
return true;
} catch (NotFoundException $e) {
$user = new User();
continue;
} catch (ValidationException $e) {
$user->setName($this->stripInvalidChars($name));
continue;
} catch (PersistenceException $e) {
return false;
}
cu,
Lars
Hi Julien,
I'm still on the fence, not sure if it isn’t too opaque.
Am 26.04.2013 um 16:41 schrieb Julien Pauli jpauli@php.net:
[...]One question regarding scoping: does the next statement inherit the scope of the catch block like this?
try {
$user = $repository->findById(123);
$user->setName($name);
$em->save($user);
return true;
} catch (NotFoundException $e) {
$user = new User();
continue;
} catch (ValidationException $e) {
$user->setName($this->stripInvalidChars($name));
continue;
} catch (PersistenceException $e) {
return false;
}
In this specific case it would have to, as PHP has function-level
scoping, no block scoping.
But this really like abusing exceptions ...
johannes
HI,
I also really don't like it.
if you have a try/catch within a for, will it continue the try or for?
(or have I to use "continue 2" to reach the for) At least use another
keyword to make it more clear that you don't want to continue a loop
at the beginning but a catch statement after the exception.
I would have solved your example like this:
try {
try {
$user = $repository->findById(123);
} catch (NotFoundException $e) {
$user = new User();
}
try {
$user->setName($name);
} catch (ValidationException $e) {
$user->setName($this->stripInvalidChars($name));
}
$em->save($user);
return true;
} catch (Exception $e) {
return false;
}
I think that's more clear - but a few lines longer.
And it's probably more save, when the "$em->save($user);" throws a
NotFoundException exception then a new user will be created and then
(without saving) true is returned.
Hello internals,
I had an idea recently with a friend, about a feature try-catch blocks
could use.
Let me just write an example, you will quickly understand the idea :<?php
try {
- foo();*
- bar();*
- baz();*
} catch (SomeException $e) {- dosomestuff();*
- continue; /* Here is the feature, go back to try block /
} catch (Exception $e) {- dosomething();*
}
this has quite a few issues and feels like abusing exceptions for
regular control flow.
The primary issue is that "throw" is a terminating operation. A
developer expects the following statements not tobe executed. To ensure
this in future a developer has to write
if ($error) {
throw new Exception();
die("continue not allowed");
}
This makes writing roust library code more complex. An alternative might
be to have "recoverable exceptions"
if ($error) {
throw new RecoverableException();
// might continue here
}
but this is weird, too: I know this situation can be fixed and prepare
my code to be fixed from the outside ... so why use exceptions for that?
An alternative might be to have a "error situation fixing component"
passed in (dependency injection like)
Throwing exceptions and jumping back and forth makes the code really
hard to follow.
Those things aside there are technical complications: We can't unwind
the stack anymore during the throw operation but have to put it aside,
execute all relevant catch blocks and then either recover it or throw it
away. So looking at this code:
function foo() {
$o = new Class();
if ($o->getSituation()->isError()){
throw new Exception();
}
}
function bar() {
try {
foo();
} catch(Exception $e) {
throw $e;
}
}
try {
bar();
} catch(Exception $e) {
continue;
}
When will $o's destructor be called?
This all looks "funny" but nothing like a serious feature.
johannes
Hi!
this has quite a few issues and feels like abusing exceptions for
regular control flow.
The primary issue is that "throw" is a terminating operation. A
I agree. If your code can handle the problem, it should not throw. If it
throws, the control should not go back there, since the code already
gave up and declared it can not continue doing whatever it was doing.
Exceptions are meant to handle exceptional situations, not serve as a
kind of goto with objects, IMO, and if the code threw an exception, it
should be done.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2013/4/29 Stas Malyshev smalyshev@sugarcrm.com
I agree. If your code can handle the problem, it should not throw. If it
throws, the control should not go back there, since the code already
gave up and declared it can not continue doing whatever it was doing.
Exceptions are meant to handle exceptional situations, not serve as a
kind of goto with objects, IMO, and if the code threw an exception, it
should be done.
It's a point of view, not something the language should enforce.
You may have a lib/object/chunk of code which raises exceptions, because
its developer thought some error is not recoverable; but when you use it,
you don't want to break your program's execution.
It happens.
Hi!
It's a point of view, not something the language should enforce.
It is a point of view that any proper language should enforce. Languages
always enforce certain style, certain ideas and certain paradigm - be it
computer languages or natural languages. You can decide from now on in
your language the word "gloobloobla" would mean "hello, how are you
today?" but if you try to use it to communicate with other people they
would think you're not well. Language is a set of conventions, and this
proposal flies in the face of all conventions that are commonly used and
accepted not only in PHP but in the wider world of OO and modern
programming in general. We don't need to import Visual Basic mistakes
decades after they were made and recognized as such. See e.g.:
http://stackoverflow.com/q/11596045/214196
You may have a lib/object/chunk of code which raises exceptions, because
its developer thought some error is not recoverable; but when you use
it, you don't want to break your program's execution.
That's why you have try/catch.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
hi,
You may have a lib/object/chunk of code which raises exceptions, because
its developer thought some error is not recoverable; but when you use
it, you don't want to break your program's execution.That's why you have try/catch.
Exactly, I cannot agree more here.
This proposal brings yet again exceptions for control flow, which is
really not what they are designed for (no matter the language, or
almost all languages). An exception can be handled and the program can
continue? Catch it, any other tricks bring control flow using
exception and that's really a bad idea.
Cheers,
Pierre
@pierrejoye | http://www.libgd.org
2013/4/30 Pierre Joye pierre.php@gmail.com:
hi,
You may have a lib/object/chunk of code which raises exceptions, because
its developer thought some error is not recoverable; but when you use
it, you don't want to break your program's execution.That's why you have try/catch.
Exactly, I cannot agree more here.
This proposal brings yet again exceptions for control flow, which is
really not what they are designed for (no matter the language, or
almost all languages). An exception can be handled and the program can
continue? Catch it, any other tricks bring control flow using
exception and that's really a bad idea.
I'm quite opposed to changing try/catch flow with this additions for
the same reasons that Stas and Pierre mentioned.
Especially since this can perfectly be achieved without it and a few
more ifs/whiles/gotos.
There no need to have a gentle/nice-to-have syntax to handle the cases
of libraries abusing exceptions.
Cheers,
Patrick
The feature exists in Python:
http://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed,
in Ruby:
http://stackoverflow.com/questions/5089802/which-is-the-shortest-way-to-silently-ignore-a-ruby-exception.
Just saying.
On Wed, May 1, 2013 at 4:46 AM, Patrick ALLAERT patrickallaert@php.netwrote:
2013/4/30 Pierre Joye pierre.php@gmail.com:
hi,
On Tue, Apr 30, 2013 at 7:54 PM, Stas Malyshev smalyshev@sugarcrm.com
wrote:You may have a lib/object/chunk of code which raises exceptions,
because
its developer thought some error is not recoverable; but when you use
it, you don't want to break your program's execution.That's why you have try/catch.
Exactly, I cannot agree more here.
This proposal brings yet again exceptions for control flow, which is
really not what they are designed for (no matter the language, or
almost all languages). An exception can be handled and the program can
continue? Catch it, any other tricks bring control flow using
exception and that's really a bad idea.I'm quite opposed to changing try/catch flow with this additions for
the same reasons that Stas and Pierre mentioned.
Especially since this can perfectly be achieved without it and a few
more ifs/whiles/gotos.
There no need to have a gentle/nice-to-have syntax to handle the cases
of libraries abusing exceptions.Cheers,
Patrick
Hi!
The feature exists in
Python: http://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed,
I don't think it does what is proposed - except/pass just catches an
exception and does nothing, it does not return in the place where
exception was thrown down the stack and continues execution.
Looks like Ruby is the same thing. In PHP< you'd just do:
try {
dostuff();
} catch(Exception $e) {}
but that's not what was proposed.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
So, in this example, if, say, bar() throws a SomeException , the code
would then resume and execute baz() after the catch block.Just presenting the idea here, no RFC actually , I'm collecting
thoughts and notices.
It seems like you want application specific flow control (caller
specific) when particular exceptions are raised. This is achievable
through goto, why not use that?
<?php
class SomeException extends Exception {}
function foo() {
echo 'attempting 1' . PHP_EOL;
}
function bar($someCondition) {
echo 'attempting 2 with ' . $someCondition . PHP_EOL;
if ($someCondition == null) {
throw new SomeException('Foo foo fooey');
}
}
function baz() {
echo 'attempting 3' . PHP_EOL;
throw new Exception('Boo boo bar');
}
function fixupforbar(&$someCondition) {
$someCondition = 5;
}
/** main() **/
$someCondition = null;
try {
foo();
BAR: bar($someCondition);
baz();
} catch (SomeException $e) {
fixupforbar($someCondition);
goto BAR;
} catch (Exception $e) {
echo 'Exception caught: ' . $e->getMessage() . PHP_EOL;
} finally {
echo 'Cleaning up' . PHP_EOL;
}
-ralph