#1839 Defining simple exceptions

dobesv Fri 16 Mar 2012

I recently defined a bunch of exceptions to distinguish different error conditions:

const class ReadPendingErr : IOErr { new make(Err err) : super(err.msg, err) {} }
const class ConnectionPendingErr : IOErr { new make(Err err) : super(err.msg, err) {} }
const class UnresolvedAddressErr : IOErr { new make(Err err) : super(err.msg, err) {}  }
const class UnsupportedAddressTypeErr : IOErr { new make(Err err) : super(err.msg, err) {} }
const class AlreadyConnectedErr : IOErr { new make(Err err) : super(err.msg, err) {} }
const class EofErr : IOErr { new make(Err err) : super(err.msg, err) {} }

Ugly.

Is there a better way to do this in Fantom? Is there a way Fantom could be doing this better?

SlimerDude Fri 16 Mar 2012

const class ReadPendingErr            : IOErr { new make(Err err) : super(err.msg, err) {} }
const class ConnectionPendingErr      : IOErr { new make(Err err) : super(err.msg, err) {} }
const class UnresolvedAddressErr      : IOErr { new make(Err err) : super(err.msg, err) {} }
const class UnsupportedAddressTypeErr : IOErr { new make(Err err) : super(err.msg, err) {} }
const class AlreadyConnectedErr       : IOErr { new make(Err err) : super(err.msg, err) {} }
const class EofErr                    : IOErr { new make(Err err) : super(err.msg, err) {} }

Prettier!

dobesv Fri 16 Mar 2012

@SlimerDude: LoL. Thanks, I'll try that one out :-P

KevinKelley Fri 16 Mar 2012

Maybe use an enum class to list the error causes...

enum class IOFail
{
  ReadPending,
  ConnectionPending,
  UnresolvedAddress,
  UnsupportedAddress,
  AlreadyConnected,
  Eof
}

const class MyIoErr : IOErr { new make(IOFail cause, Err orig) : super(cause.toStr, orig) {} }

class Main {
 static Void main() {
  throw MyIoErr(IOFail.ReadPending, Err("upsie"))
 }
}

Login or Signup to reply.