diff --git a/course/section.php b/course/section.php
index e7babd8666c..5db42bf9d81 100644
--- a/course/section.php
+++ b/course/section.php
@@ -32,7 +32,29 @@ $sectionid = required_param('id', PARAM_INT);
// This parameter is used by the classic theme to force editing on.
$edit = optional_param('edit', -1, PARAM_BOOL);
-$section = $DB->get_record('course_sections', ['id' => $sectionid], '*', MUST_EXIST);
+if (!$section = $DB->get_record('course_sections', ['id' => $sectionid], '*')) {
+ $url = new moodle_url('/');
+ $PAGE->set_context(\core\context\system::instance());
+ $PAGE->set_url($url);
+ $PAGE->set_pagelayout('course');
+ $PAGE->add_body_classes(['limitedwidth', 'single-section-page']);
+ $PAGE->set_title(get_string('notfound', 'error'));
+ $PAGE->set_heading($SITE->fullname);
+ echo $OUTPUT->header();
+
+ $errortext = new \core\output\notification(
+ get_string('sectioncantbefound', 'error'),
+ \core\output\notification::NOTIFY_ERROR
+ );
+ echo $OUTPUT->render($errortext);
+
+ $button = new single_button($url, get_string('gobacktosite'), 'get', single_button::BUTTON_PRIMARY);
+ $button->class = 'continuebutton';
+ echo $OUTPUT->render($button);
+
+ echo $OUTPUT->footer();
+ die();
+}
// Defined here to avoid notices on errors.
$PAGE->set_url('/course/section.php', ['id' => $sectionid]);
diff --git a/lang/en/error.php b/lang/en/error.php
index c7b1e12ed88..b57d693aa76 100644
--- a/lang/en/error.php
+++ b/lang/en/error.php
@@ -483,6 +483,7 @@ $string['nostartdatenoenddate'] = 'A course end date can only be set if a start
$string['nostatstodisplay'] = 'Sorry, there is no available data to display';
$string['notallowedtoupdateprefremotely'] = 'You are not allowed to update this user preference remotely';
$string['notavailable'] = 'That is not currently available';
+$string['notfound'] = 'Not found';
$string['notlocalisederrormessage'] = '{$a}';
$string['notmemberofgroup'] = 'You are not a member of this course group';
$string['notownerofkey'] = 'You are not owner of this key';
@@ -529,6 +530,7 @@ $string['restricteduser'] = 'Sorry, but your current account "{$a}" is restricte
$string['reverseproxyabused'] = 'Reverse proxy enabled so the server cannot be accessed directly.
Please contact the server administrator.';
$string['rpcerror'] = 'Ooops! Your MNET communication has failed! Here\'s that error message to pass on to your administrator: {$a}';
$string['secretalreadyused'] = 'Change password confirmation link was already used, password was not changed';
+$string['sectioncantbefound'] = '
This content can\'t be found
It may have been deleted, or the URL may be incorrect.
'; $string['sectionnotexist'] = 'This section does not exist'; $string['sendmessage'] = 'Send message'; $string['sendmessagesent'] = 'Thanks for your feedback about: