-1

I'm reading a File in Java and adding all the data to a Map. I'm using JSONParser to parse the result of posting that Map to a URL. Sometimes, it throws:

Exception in thread "Validate26" java.lang.Error: Error: could not match input
at org.json.simple.parser.Yylex.zzScanError(Yylex.java:474)
at org.json.simple.parser.Yylex.yylex(Yylex.java:681)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at com.faris.kingvalidator.ValidateRunnable.run(ValidateRunnable.java:78)
at java.lang.Thread.run(Thread.java:745)

"Validate26" is the name of my thread.

I read up somewhere that this means there's an illegal character somewhere. However, I'm not sure where as I cannot handle this Error, using a try {} and catch {} does not work for this error and the Map<> contains hundreds of strings so printing out every value would take ages to find the certain characters. Instead, I decided that I only want to parse the result, filtering out any characters other than A-Z (non-capital too), 0-9, *, _, -, /, comma and period. How may I do this? Thanks in advance.

Faris Rehman
  • 129
  • 1
  • 4
  • 11
  • What do you mean, using try/catch doesn't work for this error? – erickson Jul 04 '15 at 00:11
  • It throws an Error that prints to my console, but does not get handled in my try {} and catch {} (even though I catch Exception which is all exceptions, this one throws an Error which doesn't get caught by this clause), maybe because it's not thrown by JSONParser. – Faris Rehman Jul 04 '15 at 00:16
  • I see. So does the `parse()` complete normally, and you get a result from the method? – erickson Jul 04 '15 at 00:17
  • Usually, doing this: Object objJsonResponse = jsonParser.parse(userRequest); Works. But sometimes, it throws that Error and I believe completely stops the thread, not sure. It's pretty annoying how I can't handle the exception though, as I'd like to make my table show an error occurred, but instead it just goes blank. Oh well... :/ – Faris Rehman Jul 04 '15 at 00:20
  • 1
    Does your catch clause use `catch (Error e)` or `catch (Throwable t)`? If it's just `catch (Exception ex)` you won't be able to catch it. – erickson Jul 04 '15 at 00:25
  • Wow, never knew that a Throwable wouldn't be caught by a catch Exception thing. Thanks! Will test now... – Faris Rehman Jul 04 '15 at 00:29
  • 2
    Yes, `catch (Exception ex)` will catch any subtypes of `Exception`, but `Error` and `Throwable` are supertypes; normally you wouldn't bother catching them because it means something is so seriously wrong that you can't recover. This parser should not be throwing `Error`; that was surprisingly sloppy. – erickson Jul 04 '15 at 00:31
  • Works! Thank you. I'll now be catching Throwable and not Exception. And yes, it's weird... Maybe I am throwing too much for it to handle anyway, I'm running 150 threads and sending 10 post requests in each thread then parsing the result. – Faris Rehman Jul 04 '15 at 00:31
  • 2
    @erickson Just a nit: `Error` and `Exception` are both direct subclasses of `Throwable` (your comment made it sound like `Exception` is a subclass of `Error`, which isn't true) – Dennis Meng Jul 04 '15 at 06:46

1 Answers1

1

For Java you have to use \\ to insert a single \

So try \\* for all the other chars you wish to escape in regex in java

For the thread exception problem please check this link

Community
  • 1
  • 1
buræquete
  • 14,226
  • 4
  • 44
  • 89