diff --git a/goaljobs.ml b/goaljobs.ml
index 6875ca591929eeb2380593110606adac5f17f781..4a66780c19ac5b226ac1582daf1ee487c2e76215 100644
--- a/goaljobs.ml
+++ b/goaljobs.ml
@@ -428,8 +428,18 @@ let goal_memory_exists k =
     goal_failed msg
   )
 
+let guard fn arg =
+  try fn arg; true
+  with
+  | Goal_result (Goal_failed msg) ->
+    prerr_endline ("error: " ^ msg);
+    false
+  | exn ->
+    prerr_endline (Printexc.to_string exn);
+    false
+
 (* Run the program. *)
-let init () =
+let rec init () =
   let prog = Sys.executable_name in
   let prog = Filename.basename prog in
 
@@ -474,10 +484,11 @@ Options:
   let args = List.rev !args in
 
   (* Was a goal named on the command line? *)
-  match args with
+  (match args with
   | name :: args ->
     (match get_goal name with
-    | Some fn -> fn args
+    | Some fn ->
+      exit (if guard fn args then 0 else 1)
     | None ->
       eprintf "error: no goal called '%s' was found.\n" name;
       eprintf "Use %s -l to list all published goals in this script.\n" name;