The recommended way to have an exception that can't be caught except in
specifically intended places is to extend java.lang.Throwable (or if
appropriate to the use case, java.lang.Error).  Those work just like
Exception except that they don't have to be declared in the method
signatures where they are throwable from and of course "catch (Exception
ex) ..." doesn't catch them since they are not subclasses of Exception.

-- Jim


On Wed, Oct 12, 2016 at 9:38 AM, Dinko Srkoč <dinko.sr...@gmail.com> wrote:

> On 12 October 2016 at 18:27, Søren Berg Glasius <soe...@glasius.dk> wrote:
> > This question should be asked in us...@groovy.apache.org
> >
> > And the answer to the question must be, no. But you could check if
>
> Well, strictly speaking, that's not quite true ;-)
>
>   @groovy.transform.InheritConstructors
>   class MySpecialException extends Throwable {}
>
>   try {
>       try {
>           throw new MySpecialException("foo")
>       } catch (ex) {
>           println "never got here!"
>       }
>   } catch (MySpecialException ex) {
>       println "did catch that $ex"
>   }
>
> but I don't think I would recommend that :-)
>
> Cheers,
> Dinko
>
> >
> > } catch (exception) {
> >     if(exception instance MySpecialException) throw exception
> >     println "bar caught $exception"
> > }
> >
> >
> >
> > Best regards,
> > Søren Berg Glasius
> >
> > Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> > Mobile: +45 40 44 91 88, Skype: sbglasius
> > --- Press ESC once to quit - twice to save the changes.
> >
> > From: o...@ocs.cz <o...@ocs.cz>
> > Reply: dev@groovy.apache.org <dev@groovy.apache.org>
> > Date: 12. oktober 2016 at 18.24.20
> > To: dev@groovy.apache.org <dev@groovy.apache.org>
> > Subject:  non-catchable exception?
> >
> > Hello there,
> >
> > is it possible to create an exception which will *not* be caught by a
> > general handler, only by a specific one? So that e.g., the following code
> >
> > ===
> > class MySpecialException extends Exception { /* whatever magic needed
> here
> > */ }
> > ...
> > def foo() {
> > throw new MySpecialException()
> > }
> > def bar() {
> > try {
> > foo()
> > } catch (exception) {
> > println "bar caught $exception"
> > }
> > }
> > static main(args) {
> > try {
> > bar()
> > } catch (MySpecialException special) {
> > println "special exception"
> > }
> > }
> > ===
> >
> > would print out "special exception" and *not* "bar caught..."?
> >
> > The reason is that the code I at the moment work with contains _lots_ of
> > generic try/catch harnesses at different levels of code; they generally
> > report the error caught and then go on processing the input. Now I would
> > need a „special” exception which would not be caught by any of them, to
> > abort the processing immediately.
> >
> > Adding a separate "catch (MySpecialException goup) { throw goup }"
> statement
> > to each of all those already existing harnesses -- which would be, I
> guess,
> > conceptually the right thing to do -- would be rather at the inconvenient
> > side.
> >
> > Thanks,
> > OC
> >
>

Reply via email to