groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srkoč <dinko.sr...@gmail.com>
Subject Re: Is it possible to implement in the language an `else` block in exception handling?
Date Mon, 28 Sep 2015 10:25:27 GMT
On 28 September 2015 at 11:33, Bob Brown <bob@transentia.com.au> wrote:
> I SUSPECT that we are heading towards developing “recovery blocks”, which look like:
>
> ---
> ensure acceptance test
> by primary alternate
> else by alternate 2
> .
> .
> else by alternate n
> else error
>
> ---

Oh, I see. That is interesting.

Scala has somewhat similar feature, albeit without acceptance testing
(only handling exceptions):

  Try {
    mayThrow()
  } orElse {
    alternative()
  } orElse {
    alternative2()
  } match {
    case Success(res) => res
    case Failure(e) => handleException(e)
  }

This can be implemented in groovy as a library, just as it is done is scala.

Cheers,
Dinko

>
> I think these first appeared sometime around 1965 (they were part of a specialist real-time
language called CORAL, I believe…also a variant of Pascal called “Pascal Plus”, if memory
serves...).
>
> Not sure that they were successful…I suspect that they required too much from the run-time
support infrastructure. Times change and they may be possible with our MUCH faster hardware.
>
> There are lots of learned academic papers discussing how this is A Good Idea…
>
> BOB
>
>
>
>
>
>
> On 28/09/2015 6:59 pm, "Dinko Srkoč" <dinko.srkoc@gmail.com> wrote:
>
>>On 28 September 2015 at 10:22, Edinson E. Padrón Urdaneta
>><edinson.padron.urdaneta@gmail.com> wrote:
>>> On Mon, Sep 28, 2015 at 3:22 AM, Maarten Boekhold <boekhold@gmx.com> wrote:
>>>>
>>>> Just put the 'else'  code at the end of the try block!
>>>>
>>>> Maarten
>>>
>>>
>>> That's not a satisfactory answer for my question. Besides, what would happen
>>> if the code that throws the exception is part of a return sentence? What if
>>> the 'else code' that is after the code that throws the exception throws one
>>> of its own and it's captured by the catch block(s)? In the other hand, isn't
>>> more clear to have inside the try block only the code that should be 'tested
>>> for an exceptional situation'?
>>
>>If I understand you correctly, what you're proposing would look a bit like this:
>>
>>  try {
>>      mayThrow()
>>  } catch (e) {
>>      handleException(e)
>>  } else {
>>      try {
>>          shouldNotThrowButOneNeverKnows()
>>      } catch(e) {
>>          handleExceptionInElse(e)
>>      }
>>  } finally {
>>      cleanup()
>>  }
>>
>>I have two questions:
>>
>>* `else` as a keyword implies that the `else` block is executed
>>instead of another code block. What would that another block be?
>>* if `try` block is the last one in a method, it'll be evaluated as an
>>expression and the result of the last expression inside try/catch is
>>the method's return value. What would be the return value in your
>>proposal?
>>
>>Cheers,
>>Dinko
>

Mime
View raw message