Article View: borland.public.jbuilder.java.language
Article #17222Re: Throws
From: Peter Peld�n
Date: Thu, 03 Dec 1998 00:00
Date: Thu, 03 Dec 1998 00:00
74 lines
2525 bytes
2525 bytes
In the very interesting article <366732FB.D0235A7F@interlog.com>,
shahidj@interlog.com made this clever claim...
> Okay I did that but have a look at what exactly I did. I am writing a
> function pop(). I am not using Stacks or Vectors, I am using linked
> lists. Basically I have wrote the methods that are used by Stack.
>
> Here it is:
>
> Hello,
>
> I have done the following
>
> public Object pop() throws EmptyStackException {
> holdings = tempo.data;
> if (top.get_next() == null) {
> top = null;
> tempo = null;
> temp = null;
> throw new EmptyStackException();
> }
>
> else {
> tempo = tempo.get_next();
> top.set_next(null);
> top = tempo;
> }
> return holdings;
>
> }
>
> This doen't seem to work, it says the following:
>
> java.lang.NullPointerException
> at LinkedStack.pop(Compiled Code)
> at LinkedStack.pop(Compiled Code)
> at Assignment4.interactiveTest(Compiled Code)
> at Assignment4.main(Assignment4.java:31)
>
> Is anything wrong anywhere?
>
> Thanks
> Shahid
>
>
>
> Steve Schafer (TeamB) wrote:
>
> > On Thu, 03 Dec 1998 12:54:00 -0800, Shahid Juma <shahidj@interlog.com>
> > wrote:
> >
> > > How do you throw an exception such as EmptyStackException? Do you
> > >do something like this:
> > >
> > >public Object test (Object d) throws EmptyStackException {
> > >}
> > >
> > >I tried that and it doesn't do it.
> >
> > That declaration tells the compiler that the test method is _capable_
> > of throwing an exception. To actually throw the exception somewhere
> > within the body of the method:
> >
> > throw new EmptyStackException();
> >
> > -Steve
>
>
>
>
It seems to me that you object top throws a NullPointerException instead
of returning null when there's nothing to return. (I believe this to be
quite common)
Try catching that instead of checking for null-refs.
Peter
Technical Details
Message-ID:
<MPG.10d135048c58dc48989685@forums.inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3666FA68.1AE15DBC@interlog.com> <3732fbbd.825410610@90.0.0.40> <366732FB.D0235A7F@interlog.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,32517523289d282 X-Google-Attributes: gidf74b3,public From: Peter.Peldan@ellemtel.se (Peter Peld�n) Subject: Re: Throws Date: 1998/12/03 Message-ID: <MPG.10d135048c58dc48989685@forums.inprise.com> X-Deja-AN: 418402695 References: <3666FA68.1AE15DBC@interlog.com> <3732fbbd.825410610@90.0.0.40> <366732FB.D0235A7F@interlog.com> Organization: Ellemtel Newsgroups: borland.public.jbuilder.java.language