Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.7K Views
|
1 Reply
|
1 Total Like
View groups...
Share
Share this post:

Capture the error triggered by Failure programmatically

Posted 3 years ago

In my script, I use the following code snippet:

If[count == max, 
Failure["StepsExceeded",  <|"MessageTemplate" -> "Maximum steps exceeded the limit `Number`","MessageParameters"-> <|"Number" -> max|>|>], 
grp]

I want to know how to capture the error triggered by Failure programmatically. Furthermore, is there a better way to deal with such problems in order to facilitate the control and arrangement of complex logic in programming?

Regards,
Zhao

POSTED BY: Hongyi Zhao
Posted 3 years ago

One way is to use FailureQ

count = max = grp = 1;
f := If[count == max, 
  Failure["StepsExceeded", <|
    "MessageTemplate" -> "Maximum steps exceeded the limit `Number`", 
    "MessageParameters" -> <|"Number" -> max|>|>], grp]

If[FailureQ@f, "It failed"]

Also see this and this.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard