# How do I test for the occurrence of an error message and act on it?

I want to see how well some curves fit a data set. If a curve doesn't fit after 100 iterations, I get the error cvmit. How do I test for and handle this error within my code?

-

I think Check will do it, e.g.:

 cvmitErrorFlag=False;
Check[
(* function that may throw error *)
Fit[ data, funs, vars ],
(* what to do if error is thrown *)
Print["No convergence within 100 iterations!"];
cvmitErrorFlag=True,
(* limit to only this error *)
Fit::cvmit
]


Of course, you could be using other functions to do the fitting (instead of Fit). You could also do things like Quiet the error message, in case you want to handle the convergence problem gracefully.

-