Fix to set state.error before throw the exception

When the exception is thrown without setting the value of state.error, the application exits.