I am using JsonPath and am able to parse my data and get the values when the path provided is correct.

However when I provide an incorrect path (user input) the program terminates. I dont want that to happen. Is there anyway for me to catch this exceptions and continue with the next steps?

try
{
String value = JsonPath.read(jsonText, jsonPath); System.out.println(value);
}
catch(InvalidPathException e)
{
System.out.println("ERROR<InvalidPathException>: "+e.getMessage());
}

I get following error with invalid path:

Exception in thread "main" com.jayway.jsonpath.InvalidPathException: invalid path
at com.jayway.jsonpath.internal.filter.FieldFilter.filter(FieldFilter.java:59)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:182)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
at office.jsonPathparse.main(jsonPathparse.java:37)

This exception seems to be comming from FieldFilter which is jar's class.

If I understood you correctly and your catch block isn't being executed, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathException.
Double check your import statements.

Java JsonPath grab InvalidPathException in code, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathExceptio的更多相关文章

  1. 使用java类加载器,报异常java.nio.file.InvalidPathException

    String path = Label.class.getClassLoader().getResource("").getPath(); /F:/idea-Java/ImageD ...

  2. java.io.StreamCorruptedException: invalid type code: AC错误的解决方法

    问题描述: 在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流 ...

  3. Java Server returned HTTP response code: 401

    今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...

  4. Java会出现"unreachable code"错误的几个例子

    public class exam { static int num=5; static int m1(){ try{ num=6; throw new NullPointerException(); ...

  5. [java bug记录] java.util.zip.ZipException: invalid code lengths set

    1. 描述:将代码迁移到maven工程,其中用ZipInputStream读取/src/main/resources下的zip文件时报错:“java.util.zip.ZipException: in ...

  6. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  7. java was started but exit code =-805306369

       打开STS 时报  java was started but exit code =-805306369这个错,一个页面. 原因我把STS里面的默认jdk换成了7.但是STS的ini文件里依赖的 ...

  8. 阿里巴巴 fastjson-1.2.12.jar json解析异常java.lang.ClassFormatError: Invalid method Code length 66865 in class file com/alibaba/fastjson/serializer/ASMSerializer_6_UserKdlb

    承接上篇:fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:1 ...

  9. 小师妹学JVM之:java的字节码byte code简介

    目录 简介 Byte Code的作用 查看Byte Code字节码 java Byte Code是怎么工作的 总结 简介 Byte Code也叫做字节码,是连接java源代码和JVM的桥梁,源代码编译 ...

随机推荐

  1. C#调取接口时报错:服务器提交了协议冲突. Section=ResponseStatusLine

    private Dictionary<string, Object> GetLocation(string imei) { #region===代码=== string serviceAd ...

  2. 通过queue实现前端的被动接收

    一般请求都是由前端主动发起请求,后端响应,但有些情况必须要后端达到一定条件了才向前端相应数据,这就变成前端被动了.比如微信接收信息,只有别人给你发消息,你才能被动接收消息. 最近做了个项目,当有人经过 ...

  3. python阳历转农历

    # 引入日历库模块 import sxtwl # 日历中文索引 ymc = ["十一", "腊", "正", "二", ...

  4. python学习第三十六天命名空间的概念

    python命名空间也叫名字空间,也叫名称空间,任何编程语言都有命名空间,大体意思都一样,定义文件所在的目录,下面详细讲述命名空间几种情况 1,locals: 是函数内的名称空间,包括局部变量和形参 ...

  5. 【Spring Boot项目】Win7+JDK8+Tomcat8环境下的War包部署

    一.pom.xml及启动类修改 pom.xml Step1:指定打包类型 <!-- 打包类型 jar 或 war --> <packaging>war</packagin ...

  6. JavaScript原型&原型链

    原型&原型对象 先来一段简单的代码: function Fun(name) { this.name = name } var obj = new Fun('obj') JavaScript中的 ...

  7. python-内置常量

    引言 Python的内置常量不多,只有6个,分别是True.False.None.NotImplemented.Ellipsis.__debug__ 一.True 1.True是bool类型用来表示的 ...

  8. Jumpserver安装过程

    Jumpserver 安装过程 可参照此官方文档搭建: http://docs.jumpserver.org/zh/docs/step_by_step.html 其中,需注意处: # docker   ...

  9. 微信小程序(16)-- bindtap,catchtap事件绑定的区别

    bindtap,catchtap事件绑定的区别,这里就涉及冒泡事件了.bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. logs.wxml <view cl ...

  10. 三、SQL Server 对JSON的支持

    一.SQL Server 对JSON的支持 一.实现效果   现在 我用数据库是sql2008 ,共计2万数据. 每一条数据里面的有一个为attribute字段是 json存储状态属性,  我怎么查看 ...