Dear members, I have the following code: > tryCatch(function() print("fred"), error = function(e) sum(1:3), finally = sum(1:3)) function() print("fred") The expected output from the tryCatch call should be to print "fred" to the console, and exit, but as seen above, it is outputting function() print("fred") Can you people please shed some light on what is happening? thanking you, Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]]
inline On Wed, Jun 22, 2022 at 12:01 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear members, > I have the following code: > > > tryCatch(function() print("fred"), error = function(e) > sum(1:3), finally = sum(1:3)) > function() print("fred") > > The expected output from the tryCatch call should be to print "fred" to > the console,NO! You have simple defined the function -- you have not called/executed it. This defines and calls it:> tryCatch({function() print("fred")}(), error = function(e) sum(1:3),finally = sum(1:3)) [1] "fred" Cheers, Bert> and exit, but as seen above, it is outputting > function() print("fred") > > Can you people please shed some light on what is happening? > > thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
You defined a function. You did not call the function. tryCatch returned the object you defined. So the interactive console printed the object returned. Invoking the "function" function does not call the defined function for you. Try: tryCatch((function() print("fred"))(), error = function(e) sum(1:3), finally = sum(1:3)) On June 22, 2022 12:00:38 PM PDT, akshay kulkarni <akshay_e4 at hotmail.com> wrote:>Dear members, > I have the following code: > > > tryCatch(function() print("fred"), error = function(e) sum(1:3), finally = sum(1:3)) > function() print("fred") > >The expected output from the tryCatch call should be to print "fred" to the console, and exit, but as seen above, it is outputting > function() print("fred") > >Can you people please shed some light on what is happening? > >thanking you, >Yours sincerely, >AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.