[Newbies] How to create new exception

Michael van der Gulik mikevdg at gmail.com
Wed Oct 15 21:19:33 UTC 2008


On Wed, Oct 15, 2008 at 5:22 PM, Alex Chi <alex_chi99 at yahoo.com> wrote:

> Sorry for the that, I mean my problem is I put the exception in the wrong
> part of my program, so everytime I try to call it in the method it does not
> return the exception, so what i did was:
>
> 1. Create an new exception class, which is: FileNotFoundError
>
> Smalltalk.Core defineClass: #FileNotFound
>     superclass: #{Core.Error}
>     indexedType: #none
>     private: false
>     instanceVariableNames: ''
>     classInstanceVariableNames: ''
>     imports: ''
>     category: ''
>
> 2. I use that new exception to raise signal on a FileNotFoundError.
>     fileNotFound
>
>         ^FileNotFound raiseSignal: 'The file is not exist'
>


Which version of Smalltalk are you using? This doesn't look like Squeak to
me.

If you want an Exception object, you could just return a new instance:

fileNotFound
   ^ FileDoesNotExistException new.

Although this method is pretty pointless.

I think you're misunderstanding how Exceptions work; you need to do more
reading. I'm not sure if Smalltalk Exceptions are well documented anywhere,
but I do know that they work in a similar way to exceptions in other
programming languages such as Java or Python, so you could Google or get
books from a library that describe these.

Briefly, Exceptions are intended for handling situations where something
goes wrong. See "FileDoesNotExistException class>>example" for an example.

Gulik.

-- 
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20081016/3aaf2988/attachment.htm


More information about the Beginners mailing list