Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this throw an exception?

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code:



And here's what it throws:



The question is: Why the hell does it throw it?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):

 
Anton Golovin
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic