this post was submitted on 29 Dec 2025
111 points (98.3% liked)

Programming

24117 readers
423 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments

Depending on the language exceptions are used in many different ways. Some use it liberally for all kinds of error handling.

A good feature of Exceptions is you can throw them all the way up the stack and handle them there, giving you loose coupling between the code that calls the dangerous code and the one that catches it.

Exceptions have a big runtime overhead, so using them for normal control flow and error handling can be a bit meh.

Using return types can be great, if the language has good support for. For example swift enums are nice for this.

enum ResultError  {
  case noAnswer;
  case couldNotAsk;
  case timeOut
}

enum Result {
  case answer: String;
  case error: ResultError
}

func ask(){
  let myResult = askQuestion(“Are return types useful?”);
  switch myResult {
    case answer: 
      print(answer);
    case error:
       handleError(error);
  }
}

func handleError(error: ResultError) {
  switch ResultError {
    case noAnswer:
      print(“Received no answer”);
    case couldNot:
      …
  }

}

Using enums and switch means the compiler ensures you handle all errors in a place you expect.