Message Boards Message Boards

Exception handling in Mathematica

Posted 5 years ago

What is the cleanest way to handle exceptions in Mathematica. For example:

(* import the Zoom report *)

ecicc = Import[SystemDialogInput["FileOpen"]];

(* convert the Zoom dates into DateObjects *) <more code goes here>

I want to let the user select a file, and then process the contents. However, if no file is selected, e.g. Cancel is clicked, I need to not execute the code. Obviously, I could wrap all the code in an If statement, but that seems very clumsy. How do Mathematica programmers handle this situation in a functional style?

POSTED BY: Andrew Burnett
2 Replies
Posted 5 years ago

Ah, that's very helpful, thank you. I didn't know about Quiet[]

Cheers Andy

POSTED BY: Andrew Burnett

Hey Andrew, I tried here.. and.. you could use the following code

test = Quiet[Import[SystemDialogInput["FileOpen"]]];
If[FailureQ@test, "Please, select a file to continue", test]
Out[2] = "Please, select a file to continue"
POSTED BY: Estevao Teixeira
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract