It's not a secret anymore!
The moose likes Java in General and the fly likes Why does this throw an exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why does this throw an exception?" Watch "Why does this throw an exception?" New topic

Why does this throw an exception?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Here's some code:

And here's what it throws:

The question is: Why the hell does it throw it?
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
That's a really good question. It definitely doesn't have to do with length.

Edit: It has to do with having a back slash at the end of the string. Remove it and you will see what I mean.
[ July 01, 2004: Message edited by: Darin Niard ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I'm not sure what's going on, but note that the documentation for replaceAll() tells us (if you follow the link to the Matcher class' method) that the replacement string is not treated literally, and backslashes have special meaning as escape characters - which is probalby not what you want. So you probably need to again double (that is, quadruple) each \ in your preplacement string literal, to get:

String APP_ROOT = "C:\\\\Documents and Settings\\\\SysOp\\\\My Documents\\\\AB LOGIC\\\\Eclipse Workspace\\\\abLogic\\\\web\\\\";

Strange, I know, but it should work. I bet it was the final \\ in the original string that caused the problem - the compiler translated that to a single \, and the regex parser saw that single \ and figured it was an escape for the following character. Except there was no following character, so it got confused.

"I'm not back." - Bill Harding, Twister
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Notice that the same is true for '$'. That's why I have written a method for escaping replace values, which goes something like (IIRC - the following is from memory, and therefore untested):

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Please note: by writing the code like that, you are possibly breaking portability. For HTML strings, instead of escaping double quotes, you may use single quotes.

Anton Golovin ( SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I agree. Here's the link:
subject: Why does this throw an exception?
It's not a secret anymore!