#1373·go

Implement new Concept Exercise: panic-recover

Author: tehsphinxCreated Mar 1, 2020Updated Jul 17, 2022
Labelsx:action/createx:knowledge/intermediatex:module/concept-exercisex:status/claimedx:type/contentx:size/large

This issue describes how to implement the panic-recover concept exercise for the Go track.

Getting started

The docs can be found here

Also, be aware of these general guidelines:

Goal

The goal of this exercise is to teach the student how to work with panic and recover in Go.

Learning objectives

  • panic with error or other value
  • recover a panic
  • check the value type of the recover value
  • don't panic: if, then only in main package to crash the program (e.g. if not all required services are reachable)
  • other things that can panic: nil pointer dereference, index out of bounds, etc.
  • A panic in the code is always a bug --> fix it! (don't use it as try .. catch alternative!)
  • global panic handling in e.g. web-servers: their reason is to prevent potential bugs from crashing the entire server

Out of scope

Nothing yet.

Concepts

  • panic-recover

Prerequisites

  • defer
  • type-assertions

Resources to refer to

Implementing

Some function(s) that panic and the panic must be recovered in another function and turned into a proper error.

As an idea on how to teach "when not to use a panic/not to handle a panic but instead fix the bug": Some functions could be already in the stub that panic on running the tests and the students task is to fix the problem. (they should fix the bug, not use recover)

Help

If you have any questions while implementing the exercise, please post the questions as comments in this issue.