Article View: borland.public.jbuilder.java.language
Article #8087Exception getMessage() string not being propagated
From: B.J. (Bryce) Brogan
Date: Tue, 24 Apr 2001 19:32
Date: Tue, 24 Apr 2001 19:32
117 lines
4130 bytes
4130 bytes
Hello,
I am having a problem when throwing an exception with
an associated String message, in that the message is not
being displayed by the .getMessage() method in the
associated catch block located in the calling procedure.
The exception is being propagated okay, but its message
string is not.
I have appended the full code and output, but the main
parts are:
Object pop() throws IndexOutOfBoundsException
{
if(cnt<0)throw new IndexOutOfBoundsException("Queue underflow");
return que[--cnt]; // testing IndexOutOfBoundsException message:
}
a call to which is caught in the caller catch block
catch(IndexOutOfBoundsException e)
{
System.err.println(e.getMessage()); // reports null ???
e.printStackTrace(System.err);
}
I have tried changing IndexOutOfBoundsException to just
Exception, and to Throwable, with the same result. I
have also tried altering the code a bit so I could display
the .getMessage() in the throwing method, and verified it
displayed okay there, but it just disappears when the
exception object is used in the caller.
Any suggestions or comments would be appreciated.
BJ
========= Code Listing of Thread3.java ==========
==== run with command line arguments a b c ======
public class Thread3
{
public static void main(String[] args)
{
int argc = args.length;
Queue q = new Queue(argc);
try
{
for(int i=0;i<argc;i++)
{
q.push(args[i]);
}
for(int i=0;i<argc;i++)
{
System.out.println(" Main: " + (String)q.pop());
}
}
catch(IndexOutOfBoundsException e)
{
// e.getMessage() contains "null" string here???
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}
class Queue
{
Object que[];
int max;
int cnt;
public Queue(int max)
{
this.max = max;
this.cnt = -1;
this.que = new Object[max];
}
void push(Object o) throws IndexOutOfBoundsException
{
if(cnt>=max)throw new IndexOutOfBoundsException("Queue overflow");
que[++cnt] = o;
}
Object pop() throws IndexOutOfBoundsException
{
if(cnt<0)throw new IndexOutOfBoundsException("Queue underflow");
return que[--cnt]; // testing IndexOutOfBoundsException message:
}
}
============== Output (JPadPro) ===============
---------------- JDK Debug Build ------------------
Compiling F:\Projects\JPadPro\Test\Thread\Thread3.java
Command line: "D:\Dev\Borland\JBuilder4\jdk1.3\bin\javac_g.exe"
-deprecation -g -classpath "F:\Projects\JPadPro"
"F:\Projects\JPadPro\Test\Thread\Thread3.java"
The current directory is: F:\Projects\JPadPro\Test\Thread
Finished
Starting application F:\Projects\JPadPro\Test\Thread\Thread3.class
Command line: "D:\Dev\Borland\JBuilder4\jdk1.3\bin\java_g.exe"
-classpath "F:\Projects\JPadPro" Test.Thread.Thread3 a b c
The current directory is: F:\Projects\JPadPro\Test\Thread
Main: b
Main: a
null
java.lang.ArrayIndexOutOfBoundsException
at Test.Thread.Queue.pop(Thread3.java:62)
at Test.Thread.Thread3.main(Thread3.java:25)
Interactive Session Ended
Technical Details
Message-ID:
<71lbet89e489sk39i3767euiciasauroet@4ax.com>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!RRZ.Uni-Koeln.DE!nntp.CS.Uni-Magdeburg.De!PeerFeed.KWeb.on.ca!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,737684ad17c39486,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-24 12:40:04 PST From: "B.J. (Bryce) Brogan" <broganREMOVEME@cadvision.com> Newsgroups: borland.public.jbuilder.java.language Subject: Exception getMessage() string not being propagated Date: Tue, 24 Apr 2001 13:32:50 -0600 Message-ID: <71lbet89e489sk39i3767euiciasauroet@4ax.com> X-Newsreader: Forte Agent 1.8/32.548 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.67.67.196 X-Trace: dnews 988140768 24.67.67.196 (24 Apr 2001 12:32:48 -0700) Lines: 118 Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!RRZ.Uni-Koeln.DE!nntp.CS.Uni-Magdeburg.De!PeerFeed.KWeb.on.ca!dnews