I don't think cron renumber courses. course-ids remain the same. Deleted courses-ids return "Can not find data record in database table course."
if $course not set, try if (!isset($courses)) {...}.
get_course if id not empty return $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
and that trows an dml_missing_record_exception.
General developer forum
Course Doesn't Exist - Moodle 2.9
This discussion has been locked so you can no longer reply to it.