The Artima Developer Community
Sponsored Link

Java Answers Forum
try/catch help

5 replies on 1 page. Most recent reply: Dec 2, 2002 2:13 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

try/catch help Posted: Dec 2, 2002 7:30 AM
Reply to this message Reply
Advertisement
I have a Java program that is exhibiting some odd behavior. The problem I'm having that puzzles me is that after I put some statements in a try/catch block, hoping to track down any unforseen errors, the program doesn't work as expected. If I remove the try/catch, all is well. What's going on? What does this indicate?

Thanks!

Lynn.


Antonas

Posts: 11
Nickname: tony
Registered: May, 2002

Re: try/catch help Posted: Dec 2, 2002 7:53 AM
Reply to this message Reply
Its pretty unclear what You mean by your java app is not working improper. Can You copy/paste fragment and add what happening with and what without try/catch.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: try/catch help Posted: Dec 2, 2002 8:34 AM
Reply to this message Reply
All right, but this is kind of a mess - it's for testing - and a fragment doesn't make a lot of sense, but if I'm inadvertantly doing something, I'd like to know about it! So here goes:

	try 
	{
	userid = (String) person.getAttribute("username");
	long date = (System.currentTimeMillis())/1000;
	String timestr = Long.toHexString(date);
	byte[] datetime = Hex.fromString(timestr);
	byte [] userid1 = userid.getBytes("US-ASCII");
	int dlength = datetime.length;
	int ulength = userid1.length;
	int mlength = dlength + ulength;
	byte[] mixture = new byte[mlength];
	System.arraycopy(datetime,0,mixture,0,datetime.length);	
	System.arraycopy(userid1,0,mixture,dlength,ulength);
	}
	catch (NullPointerException npe)
        {
        npe.printStackTrace();
	}

("Hex.fromString()" is a Cryptix routine, and that stuff with "person" is from uPortal routines - two packages I use. Basically, what I'm doing here is setting up a userid and date/time string to be encrypted)

In looking at the changes I'd have to make to my program to put it "into production", I felt it would be a good idea to have a try/catch block over this code, so I put it in - only to have my program then tell me that ultimately, the encryption failed. Since the try/catch was the only thing I'd changed, I commented that out and re-compiled and re-ran - and everything was fine! But why would the insertion of a try/catch construct have any effect on execution, as long as there's no error involved?

Lynn.

Antonas

Posts: 11
Nickname: tony
Registered: May, 2002

Re: try/catch help Posted: Dec 2, 2002 9:58 AM
Reply to this message Reply
Well its really pretty strange if some method call fails because of adding try/catch. Try/catch actualy creates so called guarded region and ataches erorr handler to it (catch(){} in java case )
Are You sure that Your convertion failure is not printed Stack trace of runtime nullPointerException ?
One suggestion could be that method which includes fragment throws NullPointerException and its handled somewhere else and now You catch it inside the method. But StackTrace should be printed anyway.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: try/catch help Posted: Dec 2, 2002 11:56 AM
Reply to this message Reply
Nope, there's no stack trace that I can find - not from the command line, not from any logging programs. It just doesn't make sense! But...hmm...the try/catch construct would define a scope for the variables, wouldn't it? So something that's within it wouldn't be defined outside it, right? That COULD be what's happening...I don't really need the try/catch anyway, but it looks nice - it'd be nice if I could keep it there. I was thinking that maybe I'd stumbed on to something that I misunderstood long ago...

Thanks for your help and time!

Lynn.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: try/catch help Posted: Dec 2, 2002 2:13 PM
Reply to this message Reply
Yes, it is creating a scope and you are declaring several variables there; if you've used any of those outside the try, then that would be a problem, but I think it would be a compile failure.

It is theoretically possible that you are so close to running out of stack space that doing the try/catch pushes you over the top, but that is unlikely and would be JVM-specific.

It seems like it may just be a fringe effect of the scoping. Maybe you could experiment with creating the scope, but not the try and catch (that is, just put the curlies around the code). Does that have the same problem?

I think it is better to have the exception handling there than not. If you don't have time to figure out the problem, then you should at least add a clear comment explaining the problem and why you don't have exception handling there -- you'll thank yourself later for it!

Flat View: This topic has 5 replies on 1 page
Topic: Sun Certification Previous Topic   Next Topic Topic: Getting values from an array.........

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use