Filter out singularity issues in sim1

Consider the following mod-file

% Basic RBC Model 
% Linearization in level
%
% 
%----------------------------------------------------------------
% 0. Housekeeping (close all graphic windows)
%----------------------------------------------------------------

close all;


%----------------------------------------------------------------
% 1. Defining variables & parameters;Calibration
%----------------------------------------------------------------

var ly lc lk li lh lw r lg theta;
varexo e_theta e_g;

parameters beta alpha gamma delta rho_theta rho_g;

beta    = 0.99;
alpha   = 0.3;
gamma   = 0.5;
delta   = 0.025;
rho_theta = 0.95;
rho_g   = 0.9;

%----------------------------------------------------------------
% 2. Model
%----------------------------------------------------------------

model; 

  %Production Function
  ly=theta+alpha*lk(-1)+(1-alpha)*lh;

  %Consumption Euler equation
   1=beta*exp(r(+1)+lc-lc(+1));

  %Leisure-Consumption Tradeoff
   lw-lc=(-gamma)*lh;

  %Capital Accumulation
  exp(lk-lk(-1)) =1-delta+exp(li-lk(-1));

  %Resource constraint
  exp(lc-ly)+exp(li-ly)+exp(lg-ly)=1;

  %Real Wage
   exp(lw)=(1-alpha)*exp(ly-lh) ;

  %Return to Capital
   exp(r)=alpha*exp(ly-lk(-1))+1-delta;

 %Technology Shock
  theta=rho_theta*theta(-1)+e_theta;

 %Government Spending Shock
  lg=rho_g*lg(-1)+e_g;

end;

%----------------------------------------------------------------
% 3. Initial Values & Shocks
%----------------------------------------------------------------

initval;
  ly = -1.4653;
  lc = -1.4758;
  lk = -2.3207;
  li = -6.0099;
  lh =  -1.0986;
  lw = -0.7234;
  r  = 0.0101;
  lg = 0; 
  theta = 0;
end;
steady;
check;

shocks;
var e_theta; periods 1:10;
values 0.01;
end;

%----------------------------------------------------------------
% 4. Computations
%----------------------------------------------------------------

simul(periods=200);

It contains a fatal mistake: the parameter gamma must be negative, otherwise the BK-conditions are not satisfied. Running sim1 results in matrix singularity with almost all residuals being NaN. But the check for
err = max(abs(res)); is insensitive to NaNs. Thus, although the dynamics cannot be correctly computed, it nevertheless says that convergence is obtained. We should filter out such cases. However, I am not sure how to do this. Can there be cases where residuals with NaN appear but the estimation still converges? In this case, we should check at the end of sim1 if the solution contains NaNs and adapt the exit message. Or should we simply attach a penalty to NaN residuals?