【Java中的try-catch异常处理机制】教程文章相关的互联网学习教程文章

java – 使用try / catch时无限循环问题【代码】

我有一个while循环,它应该捕获一个非int输入并要求用户重新输入一个int.然而,它只是无休止地循环错误消息.有没有人知道为什么第二次不允许扫描仪输入?while(!enteredInt){try{locus = input.nextInt();enteredInt = true;}catch (Exception e){//user didn't type an integerSystem.out.println("You didn't enter a valid number, re-enter point where you will build your city");}}解决方法:当您输入不是数字的令牌时,调用nex...

Java:try-catch之后缺少return语句【代码】

参见英文答案 > Java Compiler Error: Missing Return Statement 2个我在Triangle类中使用下面的代码,允许用户设置声明的Triangle的第一个,第二个或第三个点.public Point get(String p) throws IllegalArgumentException {IllegalArgumentException e = new IllegalArgumentException();try {if (p == "first") { return first; }else if (p == "second") { return second; }else if (p == "t...

Java不可达catch块编译器错误【代码】

为什么在Java中我们可以捕获Exception,即使它没有被抛出,但是我们无法捕获它的子类(除了“unchecked”RuntimeExceptions及它的子类).示例代码:class Test {public static void main(String[] args) {try {// do nothing} catch (Exception e) {// OK }try {// do nothing} catch (IOException e) {// COMPILER ERROR: Unreachable catch block for IOException.//This exception is never thrown from the try statemen...

java – 为什么在catch InterruptException块中调用Thread.currentThread.interrupt()?

为什么在catch块中调用Thread.currentThread.interrupt()方法?解决方法:这样做是为了保持状态. 捕获InterruptException并吞下它时,实际上会阻止任何更高级别的方法/线程组注意到中断.这可能会导致问题. 通过调用Thread.currentThread().interrupt(),可以设置线程的中断标志,因此更高级别的中断处理程序会注意到它并且可以正确处理它. Java Concurrency in Practice在第7.1.3节“响应中断”中对此进行了更详细的讨论.它的规则是:O...

返回如何在try,catch,最后在Java中工作?【代码】

我无法准确理解返回在try,catch中是如何工作的. >如果我尝试并最终没有捕获,我可以在try块内放回.>如果我试试,赶上,最后,我不能在try块中放回.>如果我有一个catch块,我必须把返回到try,catch,finally块之外.>如果我删除catch块并抛出异常,我可以将返回放在try块中. 它们如何正常工作?为什么我不能把回报放在try块中? 代码与try,catch,最后public int insertUser(UserBean user) {int status = 0;Connection myConn = null;Prepar...

java – 如何在Android中使用RestClientException catch【代码】

我有一个Rest Web服务,我从下面的android代码中使用这个Web服务. Web服务工作正常我已经测试过了. 但是在下面的代码中我得到了错误String url ="http://10.0.2.2:29512/Restweb_2/webresources/generic/shirish";RestTemplate resttemplate = new RestTemplate();Log.v("in1", "in1");resttemplate.getMessageConverters().add(new StringHttpMessageConverter());Log.v("in2", "in2");String result;try { result = re...

Java如何访问try-catch块之外的变量【代码】

我是java的初学者,正在玩try-catch块.但是,我无法在try-catch块之外获取变量. 以下代码有效.class factorial{ public static void main(String[] args){try {int num = Integer.parseInt(args[0]);System.out.println(num );}catch(Exception e){System.out.println(e+" Cannot convert arg to int, exiting..");}} }但以下不起作用.class factorial{ public static void main(String[] args){try {int num = Integer.parseInt(arg...

java – SonarQube – 为什么在catch语句上调整Coverage指标【代码】

我有像java的代码try (FileInputStream fileInputStream = new FileInputStream(filePath);PrintWriter out = response.getWriter()) {while ((i = fileInputStream.read()) != -1) {out.write(i);}} catch (IOException e) {throw new IOException(e);}为此,我编写了所有测试,并使用Sonarqube作为代码覆盖工具.但不幸的是,有条件的覆盖率结果(测试涵盖了2个条件)将在我的捕获声明中出现.据我所知,有条件保险仅适用于条件检查.为什...

java – 编写Junit测试以覆盖异常和catch块【代码】

我已经为以下函数编写了Junit测试用例.当检查JACOCO测试覆盖率时.它显示只有try块被测试用例覆盖.我是编写测试用例的新手.如何在测试用例中涵盖异常和catch块 这是一种方法public static List<Student> readCsvFile(String fileName){BufferedReader fileReader = null;//logic to read file } catch (Exception e) {System.out.println("Error in CsvFileReader !!!");e.printStackTrace();} finally{try{fileReader.close();...

java – try-catch-finally vs抽象方法【代码】

在我们的系统中,我们有一个抽象类,我们称之为BasicAction,它包含几个抽象方法.其中最重要的是执行.它处理来自JSP页面的请求.主处理程序的工作方式如下:// Sample 1: String actionName;// The name of action to use Map<String, BasicAction> mapping;//The mapping of names and actual handlersBasicAction action = mapping.get(actionName);try {action.execute(request);//Handle the http request from the JSP page } ca...

Java – 我可以反思地发现我是否从catch块中的函数调用了partifuclar异常?【代码】

首先,在我被大吼之前,我想要明确一些事情:我没有计划编写任何能够使用这种控制流程的代码;它显然很可怕.忘记一个用例,这是一个关于它是否可能的问题: 我想知道是否有可能知道调用堆栈的某个位置,是否在catch块中为特定异常调用了一个函数. 假设我有一个功能:foo() { try {something } catch (SuperException s) {bar() }因此,如果没有对bar的方法签名的控制,并且给定的bar也被许多其他函数调用,那么如果在catch块中调用超级函数...

java – 专业程序员使用的样式,使用try和catch或在代码中添加错误处理?【代码】

哪一个应该优先于另一个?例:class Example {int[] A = new A[5];void setArray(int item, int index) {if(index < 0 || index >= 5) {System.out.println("Index out of bounds");} else {A[index] = item;}} }还是这个?class Example {int[] A = new A[5];void setArray(int item, int index) {try {A[index] = item;} catch (ArrayIndexOutOfBoundsException e) {System.out.println("Index out of bounds");}} }解决方法:当一...

java – 为什么包装异常仍然请求try-catch,即使在添加throws时也是如此?【代码】

我在自己的个人图书馆里使用SikuliX’s API.我的想法是在外部项目中单独引用我的库,其中包含我需要的SikuliX部分. 现在,SikuliX抛出了一个FindFailed异常,我需要它.我试着这样做:public class FindFailed extends org.sikuli.script.FindFailed {FindFailed(String msg) { super(msg); } }这似乎有道理.但是,当尝试在其中一个方法中使用throws语句时:import org.mylibrary.exceptions.FindFailed;public static boolean clickFin...

java – 如何在执行catch块时设置默认操作?【代码】

出于调试目的,我希望我的Java应用程序在执行catch子句时在MySql数据库上上载日志文件,而不管抛出哪个异常. 我认为最糟糕的解决方案是将我的上传器方法添加到我的代码中的每个catch中(正如我已经为创建我的日志文件所做的那样).这显然不是优雅的方法,只要它重复代码.所以我的问题是:它存在(或许在项目中)可以设置默认操作,只要满足catch子句就会执行它?解决方法:一种选择是使用日志框架(无论你是自己编写还是使用现有的解决方案),...

理清Java中try-catch-finally带return的执行顺序【代码】【图】

前言:try-catch-finally带return和异常时,它们之间执行顺序问题是留下来的一个小疑问,今天搞清楚它们 第一种情况:无异常 //1.try-catch-finally都带有returnpublic static int method_1(){int num = 1;try { System.out.println("---try run---");return ++num;} catch (Exception e) {System.out.println("---catch run---");return --num;}finally {num = 10;System.out.println("---finally run---");//ret...