Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18451 > unrolled thread

exception for invalid return value

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2020-12-07 10:59 +0100
Last post2020-12-08 10:28 +0100
Articles 5 — 2 participants

Back to article view | Back to comp.lang.php


Contents

  exception for invalid return value alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-12-07 10:59 +0100
    Re: exception for invalid return value Jerry Stuckle <jstucklex@attglobal.net> - 2020-12-07 10:58 -0500
    Re: exception for invalid return value Jerry Stuckle <jstucklex@attglobal.net> - 2020-12-07 16:00 -0500
      Re: exception for invalid return value alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-12-08 10:25 +0100
        Re: exception for invalid return value alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-12-08 10:28 +0100

#18451 — exception for invalid return value

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-12-07 10:59 +0100
Subjectexception for invalid return value
Message-ID<rqkudo$1vbd$1@gioia.aioe.org>
final class BoolVsThrow {

     final public static function rewind($stream, $exception) {
         if (!rewind($stream)) {
             throw $exception;
         }

         return $stream;
     }

}

The problem is that I don't know which exception pass to the method 
BoolVsThrow::rewind().

Some examples:

BoolVsThrow::rewind($stream, new \Exception);
BoolVsThrow::rewind($stream, new \InvalidArgumentException);
BoolVsThrow::rewind($stream, new \UnexpectedValueException);

The most appropriate exception?

[toc] | [next] | [standalone]


#18456

FromJerry Stuckle <jstucklex@attglobal.net>
Date2020-12-07 10:58 -0500
Message-ID<rqljf8$jfb$1@jstuckle.eternal-september.org>
In reply to#18451
On 12/7/2020 4:59 AM, alex wrote:
> final class BoolVsThrow {
> 
>      final public static function rewind($stream, $exception) {
>          if (!rewind($stream)) {
>              throw $exception;
>          }
> 
>          return $stream;
>      }
> 
> }
> 
> The problem is that I don't know which exception pass to the method 
> BoolVsThrow::rewind().
> 
> Some examples:
> 
> BoolVsThrow::rewind($stream, new \Exception);
> BoolVsThrow::rewind($stream, new \InvalidArgumentException);
> BoolVsThrow::rewind($stream, new \UnexpectedValueException);
> 
> The most appropriate exception?

You normally throw an exception related to the reason for the error. 
However, rewind() only returns true or false and not a reason for its 
failure so there is no "correct" answer.  It all depends on how you want 
to handle it.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#18457

FromJerry Stuckle <jstucklex@attglobal.net>
Date2020-12-07 16:00 -0500
Message-ID<rqm565$5eo$1@jstuckle.eternal-september.org>
In reply to#18451
On 12/7/2020 4:59 AM, alex wrote:
> final class BoolVsThrow {
> 
>      final public static function rewind($stream, $exception) {
>          if (!rewind($stream)) {
>              throw $exception;
>          }
> 
>          return $stream;
>      }
> 
> }
> 
> The problem is that I don't know which exception pass to the method 
> BoolVsThrow::rewind().
> 
> Some examples:
> 
> BoolVsThrow::rewind($stream, new \Exception);
> BoolVsThrow::rewind($stream, new \InvalidArgumentException);
> BoolVsThrow::rewind($stream, new \UnexpectedValueException);
> 
> The most appropriate exception?

Actually I think I misread your question.

You can pass it type of exception you want this routine to handle.  It 
will rewind the stream and rethrow the exception.

The beauty of exception handling is you can specify what type of 
exception to handle and therefore have multiple handlers for a routine. 
  For instance..


try {
    ...
}
catch (MyException1 $e) {
...
}
catch (MyException2 $e) {
....
}
catch (Exception $e) {
...
}

And so on.  Now in your try block if you throw and object of type 
MyException1, the first catch block will handle it and the other two 
will be skipped.  If you throw an object of MyException2, only the 
second catch block will process it.  Any other exception will be handled 
by the third catch block.

Does this help?



-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#18458

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-12-08 10:25 +0100
Message-ID<rqngqr$8lv$1@gioia.aioe.org>
In reply to#18457
Il 07/12/20 22:00, Jerry Stuckle ha scritto:
> On 12/7/2020 4:59 AM, alex wrote:
>> final class BoolVsThrow {
>>
>>      final public static function rewind($stream, $exception) {
>>          if (!rewind($stream)) {
>>              throw $exception;
>>          }
>>
>>          return $stream;
>>      }
>>
>> }
>>
>> The problem is that I don't know which exception pass to the method 
>> BoolVsThrow::rewind().
>>
>> Some examples:
>>
>> BoolVsThrow::rewind($stream, new \Exception);
>> BoolVsThrow::rewind($stream, new \InvalidArgumentException);
>> BoolVsThrow::rewind($stream, new \UnexpectedValueException);
>>
>> The most appropriate exception?
> 
> Actually I think I misread your question.
> 
> You can pass it type of exception you want this routine to handle.  It 
> will rewind the stream and rethrow the exception.
> 
> The beauty of exception handling is you can specify what type of 
> exception to handle and therefore have multiple handlers for a routine. 
>   For instance..
> 
> 
> try {
>     ...
> }
> catch (MyException1 $e) {
> ...
> }
> catch (MyException2 $e) {
> ....
> }
> catch (Exception $e) {
> ...
> }
> 
> And so on.  Now in your try block if you throw and object of type 
> MyException1, the first catch block will handle it and the other two 
> will be skipped.  If you throw an object of MyException2, only the 
> second catch block will process it.  Any other exception will be handled 
> by the third catch block.
> 
> Does this help?

In the end I settled for
trigger_error ("Operation failed", E_USER_ERROR);

[toc] | [prev] | [next] | [standalone]


#18459

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-12-08 10:28 +0100
Message-ID<rqnh0i$8lv$2@gioia.aioe.org>
In reply to#18458
Il 08/12/20 10:25, alex ha scritto:
> Il 07/12/20 22:00, Jerry Stuckle ha scritto:
>> On 12/7/2020 4:59 AM, alex wrote:
>>> final class BoolVsThrow {
>>>
>>>      final public static function rewind($stream, $exception) {
>>>          if (!rewind($stream)) {
>>>              throw $exception;
>>>          }
>>>
>>>          return $stream;
>>>      }
>>>
>>> }
>>>
>>> The problem is that I don't know which exception pass to the method 
>>> BoolVsThrow::rewind().
>>>
>>> Some examples:
>>>
>>> BoolVsThrow::rewind($stream, new \Exception);
>>> BoolVsThrow::rewind($stream, new \InvalidArgumentException);
>>> BoolVsThrow::rewind($stream, new \UnexpectedValueException);
>>>
>>> The most appropriate exception?
>>
>> Actually I think I misread your question.
>>
>> You can pass it type of exception you want this routine to handle.  It 
>> will rewind the stream and rethrow the exception.
>>
>> The beauty of exception handling is you can specify what type of 
>> exception to handle and therefore have multiple handlers for a 
>> routine.   For instance..
>>
>>
>> try {
>>     ...
>> }
>> catch (MyException1 $e) {
>> ...
>> }
>> catch (MyException2 $e) {
>> ....
>> }
>> catch (Exception $e) {
>> ...
>> }
>>
>> And so on.  Now in your try block if you throw and object of type 
>> MyException1, the first catch block will handle it and the other two 
>> will be skipped.  If you throw an object of MyException2, only the 
>> second catch block will process it.  Any other exception will be 
>> handled by the third catch block.
>>
>> Does this help?
> 
> In the end I settled for
> trigger_error ("Operation failed", E_USER_ERROR);

In particular:

         if (!rewind($stream)) {
             trigger_error("Operation failed", E_USER_ERROR);
         }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web