public static JSONObject fromObject(Object object, JsonConfig jsonConfig)
{
if(object == null || JSONUtils.isNull(object))
return new JSONObject(true);
if(object instanceof Enum)
throw new JSONException("'object' is an Enum. Use JSONArray instead");
if((object instanceof Annotation) || object != null && object.getClass().isAnnotation())
throw new JSONException("'object' is an Annotation.");
if(object instanceof JSONObject)
return _fromJSONObject((JSONObject)object, jsonConfig);
if(object instanceof DynaBean)
return _fromDynaBean((DynaBean)object, jsonConfig);
if(object instanceof JSONTokener)
return _fromJSONTokener((JSONTokener)object, jsonConfig);
if(object instanceof JSONString)
return _fromJSONString((JSONString)object, jsonConfig);
if(object instanceof Map)
return _fromMap((Map)object, jsonConfig);
if(object instanceof String)
return _fromString((String)object, jsonConfig);
if(JSONUtils.isNumber(object) || JSONUtils.isBoolean(object) || JSONUtils.isString(object))
return new JSONObject();
if(JSONUtils.isArray(object))
throw new JSONException("'object' is an array. Use JSONArray instead");
else
return _fromBean(object, jsonConfig);
}
很好奇,问什么JSONObject的fromObject抛出异常,没有捕捉?
try{}catch(Exception e){} 把 if语句包裹起来
那我只好怒答了,以下:
先看继承关系
JSONException extends NestableRuntimeException extends RuntimeException extends Exceptionextends Throwable
源代码就不必看了
异常分类:运行时异常和非运行时异常
运行时异常都是RuntimeException类及其子类异常,这些异常是不检查异常(JVM在运行期处理的异常),程序中可以选择捕获处理,也可以不处理。
自然,检查异常就是除开RuntimeException类及其子类异常,这些异常是检查异常(JVM在编译期处理的异常),我他妈既然知道你有问题,我当然要求你处理啊,三观很正是不是!
javadoc
// Exception
/**
* The class {@code Exception} and its subclasses are a form of
* {@code Throwable} that indicates conditions that a reasonable
* application might want to catch.
*
* <p>The class {@code Exception} and any subclasses that are not also
* subclasses of {@link RuntimeException} are <em>checked
* exceptions</em>. Checked exceptions need to be declared in a
* method or constructor's {@code throws} clause if they can be thrown
* by the execution of the method or constructor and propagate outside
* the method or constructor boundary.
*/
// RuntimeException
/**
* {@code RuntimeException} is the superclass of those
* exceptions that can be thrown during the normal operation of the
* Java Virtual Machine.
*
* <p>{@code RuntimeException} and its subclasses are <em>unchecked
* exceptions</em>. Unchecked exceptions do <em>not</em> need to be
* declared in a method or constructor's {@code throws} clause if they
* can be thrown by the execution of the method or constructor and
* propagate outside the method or constructor boundary.
*/
最后TMD吐槽一下这个编辑器,是贼他妈的难用 #Markdown#,参考个鸡儿,都不全,Ctrl+K你这是Ctrl+, 简直就是扯几把犊子,瞎TM排版算了