Re: strong typing and error handling

new topic     » goto parent     » topic index » view thread      » older message » newer message

----- Original Message -----
From: "Pete Lomax" <petelomax at blueyonder.co.uk>
To: "EUforum" <EUforum at topica.com>
Subject: Re: strong typing and error handling


>
>
> On Fri, 27 Jun 2003 18:56:33 +1000, Derek Parnell
> <ddparnell at bigpond.com> wrote:
>
> WOW, Derek you blew my fuse...

Sorry about that.  Here is a working example of the sort of thing one can
do...

------------
include misc.e

sequence vTypeDefCrash vTypeDefCrash = {0}
sequence vTypeDefMsg vTypeDefMsg = {""}
integer vTypeDefErrs vTypeDefErrs = 0

global procedure ShowTypeDefErr(sequence msg, object data)
    printf(2, "Type Failure: %s\n", {msg})
    if length(vTypeDefMsg[1]) > 0 then
        printf(2, "%s\n", {vTypeDefMsg[1]})
    end if
    pretty_print(2, data, {3})
    puts(2,'\n')
    vTypeDefErrs += 1
end procedure

global procedure SetDebugMsg(sequence Msg)
    vTypeDefMsg[1] = Msg
end procedure

global procedure SetTypeDefCrash( object NewVal)
  vTypeDefErrs = 0
  if sequence(NewVal) then
    if length(vTypeDefCrash) > 1 then
       vTypeDefCrash = vTypeDefCrash[2..length(vTypeDefCrash)]
       vTypeDefMsg = vTypeDefMsg[2..length(vTypeDefMsg)]
       return
    end if
  else
    vTypeDefCrash = NewVal & vTypeDefCrash
    vTypeDefMsg = {""} & vTypeDefMsg
  end if
end procedure

global function TypeDefErr()
    return vTypeDefErrs
end function

global type Customer(object x)

  if not sequence(x) then
    ShowTypeDefErr("Customer Type must be a sequence.", x)
    return vTypeDefCrash[1]
  end if

  if length(x) != 5 then
    ShowTypeDefErr("Customer Type must have 5 fields.", x)
    return vTypeDefCrash[1]
  end if

  return 1
 end type


Customer x


  SetTypeDefCrash(1) -- Turn on crash avoidance
  SetDebugMsg("Test #1")
  -- This should not crash.
  x = 0

  SetDebugMsg("Test #2")
  -- This should not crash.
  x = {1,1,1,1}

  -- See if there were any errors found
  if TypeDefErr() > 0 then
    printf(1, "Number of Errors = %d\n", TypeDefErr())
  end if

  SetTypeDefCrash("") -- Restore it to whatever it was.

  -- Program should crash this time.
  x = {1,1,1,1}

------------

Derek

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu