1
$\begingroup$

I would like to avoid this type of message and deal with the input to ToEntity differently, if Mathematica does not recognize the entity.

ToEntity[RandomGraph[{8, 15}], "Graph"]

enter image description here

Is there a simple If command for that?

$\endgroup$

2 Answers 2

4
$\begingroup$

The usual way to handle this is a combination of Quiet and Check:

f[g_] := Quiet[Check[ToEntity[g, "Graph"], "No entity found."]]

f[PathGraph[Range[4]]]
(* Entity["Graph", {"Path", 4}] *)

f[RandomGraph[{10, 3}]]
(* "No entity found." *)
$\endgroup$
2
$\begingroup$

Iff you don't want to see a message again, you can just turn it Off:

Off[ToEntity::noentp]
$\endgroup$
1
  • $\begingroup$ Thanks, I really want a check on the outcome of ToEntity and then do something else if the entity is not known. $\endgroup$ Commented Aug 5 at 12:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.