0
$\begingroup$

In python I can write fallback values like:

def f():
   try: 
     // do smtg
     return stgm
   except:
     // error was met: return fallback
     return fallback

and can easily map that function.

In mathematica I have a dataset, I would like to append a column as a result of a function applied to a row, and throw a fallback or handle that error is function fails.

I would like to consider:

  • throw a fallback value
  • handle the error, by skipping the whole line in the Dataset (like I could do in python Pandas)

In mathematica, if there is an exception, map of the function will be aborted with a Failure. I don't know what to do with this message.

enter image description here

I looked at : Exception/Error handling using returns codes

I am looking at simpler pattern that could do:

f[row_] := row["col1"] / row["col2"] // handle try except ?

// ds is Dataset 
ds[All, Append[#, "col3" -> f[#]] &]

How to do handle try / except exeptions in Mathematica?

$\endgroup$
  • 1
    $\begingroup$ I'd start with Query and probably look at FailureAction too. If you still think you need exceptions proper then you should probably look at Catch/Throw, and you should also take a look at this too $\endgroup$ – user42582 Apr 25 at 14:59
  • $\begingroup$ related: 29321 $\endgroup$ – Kuba Apr 26 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.