Comparisment

   - compare programming languages by example

52

Declaring exceptions

The syntax for declaring new exception types.

Created by sam

Add a new answer

Either add an answer for a new language or add an alternative solution.
A description is only necessary when the code is not suitably 'obvious' or to justify an alternative approach.

Haskell

import Control.Monad.Error

data AdmittanceError
  = TooTall
  | TooShort
  | RefusedForReason String
  | RefusedForNoReason

instance Error AdmittanceError where
  noMsg      = RefusedForNoReason
  strMsg str = RefusedForReason str

Declare custom exception types which can be thrown with 'Control.Monad.Error's 'throwError' and caught with 'catchError'.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

class IHaveAnException():
  pass

Any class can be raised as a custom exception type.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.