9
$\begingroup$

How can I make a construct similar to C++ functions:

try{} catch{}

I want to use it to avoid showing a lot of errors in the plot area when for example one of the arguments becomes complex or overflows etc. I want to avoid of creating a plot if ANYTHING goes wrong, so it does not look like the one below:

Show@Graphics[Line[{{Complex[1, 1]}, {Complex[2, 2]}}]]

Mathematica graphics

Approaches like:

Check[Show[Graphics[Line[{{Complex[1, 1]}, {Complex[2, 2]}}]]], Show[Graphics[{}]]]

or

Check[Graphics[Line[{{Complex[1, 1]}, {Complex[2, 2]}}]], Show[Graphics[{}]]

also fail.

$\endgroup$
  • 1
    $\begingroup$ Is Check useful? $\endgroup$ – cormullion Oct 16 '13 at 8:16
  • $\begingroup$ I have tried, but it still plots that way, or maybe I am doing something wrong: Check[Show[Graphics[Line[{{Complex[1, 1]}, {Complex[2, 2]}}]]], Show[Graphics[{}]]] or Check[Graphics[Line[{{Complex[1, 1]}, {Complex[2, 2]}}]], Show[Graphics[{}]]] $\endgroup$ – Misery Oct 16 '13 at 8:20
  • 4
    $\begingroup$ Check will only catch kernel messages. The pink boxes you see are formatting errors generated by the front end. Have a look at rcollyer's answer here for a way to check for these errors. $\endgroup$ – Simon Woods Oct 16 '13 at 8:34
  • 1
    $\begingroup$ Try Catch[] :) $\endgroup$ – stathisk Oct 16 '13 at 8:46
  • 1
    $\begingroup$ @Zet I fail to see how Catch would work here: clearly, the last examples of OP show that the problem is that no explicit error message is generated during evaluating the expression but the result is still wrong. So how to capture such an implicit failure? I have no idea, but it is a good question. $\endgroup$ – István Zachar Oct 16 '13 at 11:43