re-throwing an interrupt condition

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

re-throwing an interrupt condition

Gábor Csárdi
Anyone knows a way to re-throw a caught interrupt condition, so that
it behaves just like the original one? I.e. no error message is
printed, but the execution is stopped of course. With just calling
stop in the error handler, it prints the error message, or, if there
is no error message (like in the original interrupt condition),
another error happens.

tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
"interrupted"; stop(e) } )
<press CTRL + C / ESC here>
#> Error: interrupted

tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
<press CTRL + C / ESC here>
#> Error in stop(e) : bad error message

Thanks,
Gabor

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

Iñaki Úcar
El sáb., 28 jul. 2018 a las 18:30, Gábor Csárdi
(<[hidden email]>) escribió:

>
> Anyone knows a way to re-throw a caught interrupt condition, so that
> it behaves just like the original one? I.e. no error message is
> printed, but the execution is stopped of course. With just calling
> stop in the error handler, it prints the error message, or, if there
> is no error message (like in the original interrupt condition),
> another error happens.
>
> tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
> "interrupted"; stop(e) } )
> <press CTRL + C / ESC here>
> #> Error: interrupted
>
> tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
> <press CTRL + C / ESC here>
> #> Error in stop(e) : bad error message

Sorry, I probably misunderstood your intention, but... simply by not
calling stop at all? This is what I see:

> tryCatch(Sys.sleep(100), interrupt = function(e) do_something <<- 1)
<press CTRL + C / ESC here>
^C>
> do_something
[1] 1

Iñaki

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

Gábor Csárdi
I don't want to return a value, I want to interrupt the computation,
that's why I need to re-throw .
But before getting back to the user I want to run some cleanup code.

G.
On Sat, Jul 28, 2018 at 5:59 PM Iñaki Úcar <[hidden email]> wrote:

>
> El sáb., 28 jul. 2018 a las 18:30, Gábor Csárdi
> (<[hidden email]>) escribió:
> >
> > Anyone knows a way to re-throw a caught interrupt condition, so that
> > it behaves just like the original one? I.e. no error message is
> > printed, but the execution is stopped of course. With just calling
> > stop in the error handler, it prints the error message, or, if there
> > is no error message (like in the original interrupt condition),
> > another error happens.
> >
> > tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
> > "interrupted"; stop(e) } )
> > <press CTRL + C / ESC here>
> > #> Error: interrupted
> >
> > tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
> > <press CTRL + C / ESC here>
> > #> Error in stop(e) : bad error message
>
> Sorry, I probably misunderstood your intention, but... simply by not
> calling stop at all? This is what I see:
>
> > tryCatch(Sys.sleep(100), interrupt = function(e) do_something <<- 1)
> <press CTRL + C / ESC here>
> ^C>
> > do_something
> [1] 1
>
> Iñaki

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

luke-tierney
In reply to this post by Gábor Csárdi
The internal code does more or less

     signalCondition(e)     ## allows for (another) handler
     cat("\n")              ## cleans up console
     invokeRestart("abort") ## jump to 'abort' restart if not handled

[for back compatibility it also runs the error option code if that is
set, but that may go away eventually].

A version of your example:

> {
+     tryCatch(Sys.sleep(100),
+              interrupt = function(e) {
+                  e$message <- "interrupted"
+                  signalCondition(e)
+                  cat("\n")
+                  invokeRestart("abort")
+     })
+     cat("next step\n")
+ }
^C
>

Best,

luke

On Sat, 28 Jul 2018, Gábor Csárdi wrote:

> Anyone knows a way to re-throw a caught interrupt condition, so that
> it behaves just like the original one? I.e. no error message is
> printed, but the execution is stopped of course. With just calling
> stop in the error handler, it prints the error message, or, if there
> is no error message (like in the original interrupt condition),
> another error happens.
>
> tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
> "interrupted"; stop(e) } )
> <press CTRL + C / ESC here>
> #> Error: interrupted
>
> tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
> <press CTRL + C / ESC here>
> #> Error in stop(e) : bad error message
>
> Thanks,
> Gabor
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

Gábor Csárdi
Great! invokeRestart("abort") is a life saver again, the second time this week!

Thanks much!

Gabor

On Sat, Jul 28, 2018 at 6:37 PM <[hidden email]> wrote:

>
> The internal code does more or less
>
>      signalCondition(e)     ## allows for (another) handler
>      cat("\n")              ## cleans up console
>      invokeRestart("abort") ## jump to 'abort' restart if not handled
>
> [for back compatibility it also runs the error option code if that is
> set, but that may go away eventually].
>
> A version of your example:
>
> > {
> +     tryCatch(Sys.sleep(100),
> +              interrupt = function(e) {
> +                  e$message <- "interrupted"
> +                  signalCondition(e)
> +                  cat("\n")
> +                  invokeRestart("abort")
> +     })
> +     cat("next step\n")
> + }
> ^C
> >
>
> Best,
>
> luke
>
> On Sat, 28 Jul 2018, Gábor Csárdi wrote:
>
> > Anyone knows a way to re-throw a caught interrupt condition, so that
> > it behaves just like the original one? I.e. no error message is
> > printed, but the execution is stopped of course. With just calling
> > stop in the error handler, it prints the error message, or, if there
> > is no error message (like in the original interrupt condition),
> > another error happens.
> >
> > tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
> > "interrupted"; stop(e) } )
> > <press CTRL + C / ESC here>
> > #> Error: interrupted
> >
> > tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
> > <press CTRL + C / ESC here>
> > #> Error in stop(e) : bad error message
> >
> > Thanks,
> > Gabor
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> --
> Luke Tierney
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa                  Phone:             319-335-3386
> Department of Statistics and        Fax:               319-335-3017
>     Actuarial Science
> 241 Schaeffer Hall                  email:   [hidden email]
> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

luke-tierney
In reply to this post by Gábor Csárdi
On Sat, 28 Jul 2018, Gábor Csárdi wrote:

> I don't want to return a value, I want to interrupt the computation,
> that's why I need to re-throw .
> But before getting back to the user I want to run some cleanup code.

If it is just cleanup you want then a finally clause or on.exit should
do, and will cover all form of non-local transfer of control.

Best,

luke

>
> G.
> On Sat, Jul 28, 2018 at 5:59 PM Iñaki Úcar <[hidden email]> wrote:
>>
>> El sáb., 28 jul. 2018 a las 18:30, Gábor Csárdi
>> (<[hidden email]>) escribió:
>>>
>>> Anyone knows a way to re-throw a caught interrupt condition, so that
>>> it behaves just like the original one? I.e. no error message is
>>> printed, but the execution is stopped of course. With just calling
>>> stop in the error handler, it prints the error message, or, if there
>>> is no error message (like in the original interrupt condition),
>>> another error happens.
>>>
>>> tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
>>> "interrupted"; stop(e) } )
>>> <press CTRL + C / ESC here>
>>> #> Error: interrupted
>>>
>>> tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
>>> <press CTRL + C / ESC here>
>>> #> Error in stop(e) : bad error message
>>
>> Sorry, I probably misunderstood your intention, but... simply by not
>> calling stop at all? This is what I see:
>>
>>> tryCatch(Sys.sleep(100), interrupt = function(e) do_something <<- 1)
>> <press CTRL + C / ESC here>
>> ^C>
>>> do_something
>> [1] 1
>>
>> Iñaki
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: re-throwing an interrupt condition

Gábor Csárdi
Good points. In my case I also want to add an extra class to the
condition, before re-throwing it.
Sorry, this was not clear in my first email, I actually just realized it now.

Gabor
On Sat, Jul 28, 2018 at 6:43 PM <[hidden email]> wrote:

>
> On Sat, 28 Jul 2018, Gábor Csárdi wrote:
>
> > I don't want to return a value, I want to interrupt the computation,
> > that's why I need to re-throw .
> > But before getting back to the user I want to run some cleanup code.
>
> If it is just cleanup you want then a finally clause or on.exit should
> do, and will cover all form of non-local transfer of control.
>
> Best,
>
> luke
>
> >
> > G.
> > On Sat, Jul 28, 2018 at 5:59 PM Iñaki Úcar <[hidden email]> wrote:
> >>
> >> El sáb., 28 jul. 2018 a las 18:30, Gábor Csárdi
> >> (<[hidden email]>) escribió:
> >>>
> >>> Anyone knows a way to re-throw a caught interrupt condition, so that
> >>> it behaves just like the original one? I.e. no error message is
> >>> printed, but the execution is stopped of course. With just calling
> >>> stop in the error handler, it prints the error message, or, if there
> >>> is no error message (like in the original interrupt condition),
> >>> another error happens.
> >>>
> >>> tryCatch(Sys.sleep(100), interrupt = function(e) {  e$message <-
> >>> "interrupted"; stop(e) } )
> >>> <press CTRL + C / ESC here>
> >>> #> Error: interrupted
> >>>
> >>> tryCatch(Sys.sleep(100), interrupt = function(e) stop(e))
> >>> <press CTRL + C / ESC here>
> >>> #> Error in stop(e) : bad error message
> >>
> >> Sorry, I probably misunderstood your intention, but... simply by not
> >> calling stop at all? This is what I see:
> >>
> >>> tryCatch(Sys.sleep(100), interrupt = function(e) do_something <<- 1)
> >> <press CTRL + C / ESC here>
> >> ^C>
> >>> do_something
> >> [1] 1
> >>
> >> Iñaki
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> --
> Luke Tierney
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa                  Phone:             319-335-3386
> Department of Statistics and        Fax:               319-335-3017
>     Actuarial Science
> 241 Schaeffer Hall                  email:   [hidden email]
> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel