luillyyyy / json-simple

Automatically exported from code.google.com/p/json-simple
1 stars 0 forks source link

Wrong Exception handling #24

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
As I see in library code:
http://code.google.com/p/json-simple/source/browse/trunk/src/org/json/simple/JSO
NValue.java#39
{{{
        public static Object parse(Reader in){
                try{
                        JSONParser parser=new JSONParser();
                        return parser.parse(in);
                }
                catch(Exception e){
                        return null;
                }
        }
}}}
The idea is to return some value or null if some problem happened,
no matter RuntimeException or Checked Exception.

But code in parser may throw Error:
http://code.google.com/p/json-simple/source/browse/trunk/src/org/json/simple/par
ser/Yylex.java#474
{{{
    throw new Error(message);
}}}
so catch-block: "catch(Exception e)" will not catch Error, and
user, me, will get some Error exception in code.

The idea of errors, as far as I know is to highlight SIGNIFICANT
problems in application, when application probably can not work
any more, e.g.:
http://java.sun.com/javase/6/docs/api/java/lang/NoClassDefFoundError.html
Application can not run if some code is not found.

But Error from json_simple may be thrown even on invalid input string,
which, I think, should be handled as some sort of RuntimeException.

Original issue reported on code.google.com by dmitry.s...@gmail.com on 24 May 2010 at 8:38

GoogleCodeExporter commented 8 years ago
Yes there are some issues with parse() and it is for backward compatibility 
only now.
Please use parseWithException() instead.

Original comment by fangyid...@gmail.com on 24 May 2010 at 10:08

GoogleCodeExporter commented 8 years ago
In this case you probably should mark 
 public static Object parse(Reader in){
method as:
 @Deprectaed

Original comment by dmitry.s...@gmail.com on 24 May 2010 at 11:09

GoogleCodeExporter commented 8 years ago
Thanks. 

Original comment by fangyid...@gmail.com on 24 May 2010 at 11:22

GoogleCodeExporter commented 8 years ago
Marked as @deprecated in r212. Sadly, we're trying to stick to JDK 1.2 for 1.x, 
so the @deprecated marker is only in Javadoc instead of actually being applied 
as an annotation.

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 3:09