Tag: exception处理

exception处理无限循环

我的问题简短而甜蜜。 我不明白为什么我的程序在捕获错误时无限循环。 我做了一个新的try-catch语句,但它循环甚至复制,粘贴和修改了之前有效的程序中的相应变量。 以下是声明本身及以下将是整个计划。 感谢您的帮助! try { input = keyboard.nextInt(); } catch(Exception e) { System.out.println(“Error: invalid input”); again = true; } if (input >0 && input <=10) again = false; } 程序: public class Blanco { public static int input; /** * @param args the command line arguments */ public static void main(String[] args) { […]

如何决定创建已检查的excpetion或未经检查的exception

我想知道如何创建并抛出已检查的exception或未经检查的exception。 例如,我有一个服务,它使用一些数据并在使用之前validation它。 在validation期间,某个字段不符合规则,我想抛出一个exception,如ValidationException()。 我怎么知道应该检查或取消选中它。 在另一种情况下,我从我的代码中调用外部Web服务,例如google stock api。 我们假设我的超时时间为3秒。 如果时间exprires我想抛出exception说BackendException()。 我怎么知道它应该是一个已检查的exception还是一个未经检查的exception。 提前致谢。

如何使用hasNextInt()来捕获exception? 我需要Int,但如果输入是字符,那就很糟糕

我一直试图阻止例外,但我无法弄清楚如何。 我试过parseInt , java.util.NormalExceptionMismatch等。 有没有人有任何见解如何解决这个问题? 由于复制和粘贴,格式化有点偏。 do { System.out.print( “How many integers shall we compare? (Enter a positive integer):”); select = intFind.nextInt(); if (!intFind.hasNextInt()) intFind.next(); { // Display the following text in the event of an invalid input System.out.println(“Invalid input!”); } }while(select < 0) 我试过的其他方法: do { System.out.print( “How many integers shall we compare? (Enter […]

编译Java代码时未报告的exceptionjava.io.IOException

我遇到此代码的问题: public class gravityv1 { public static double[] calculategravity(double[] mass, int[] diameter) { double[] gravity = new double[mass.length]; for (int n = 0; n < mass.length; n++) { gravity[n] = (6.67E-11 * mass[n]) / Math.pow((diameter[n] / 2), 2); } return gravity; } public static void print(double[] gravity, double[] mass, int[] diameter, String[] planet) { System.out.println(" […]

Spring Batch:聚合读者/作者问题

我正在尝试使用Spring批处理并实现聚合读取器(批处理文件,其中多个记录在写入时应被视为一个记录)。 以下是我的读者的代码段: public class AggregatePeekableReader implements ItemReader<List>, ItemStream { private SingleItemPeekableItemReader reader; private boolean process(T currentRecord , InvoiceLineItemsHolder holder) throws UnexpectedInputException, ParseException, Exception { next = peekNextInvoiceRecord(); // finish processing if we hit the end of file if (currentRecord == null ) { LOG.info(“Exhausted ItemReader ( END OF FILE)”); holder.exhausted = true; return false; } […]

测试预期exception,exception被抛出(它显示在输出中)但是测试失败了

嗨所以有一个车辆构造函数的测试。 测试使用没有驾驶执照的驾驶员初始化车辆,并且应该抛出exception。 代码构造函数: public Voertuig(String Merk, Datum datumEersteIngebruikname, int Aankoopprijs, int Zitplaatsen, Mens bestuurder, Mens … ingezetenen) { this.nummerplaat = div.getNummerplaat(); this.Zitplaatsen = Zitplaatsen; try { this.Merk = Merk; this.datumEersteIngebruikname = datumEersteIngebruikname; this.Aankoopprijs = Aankoopprijs; if (!Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.B) || !Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.BE)) { throw new MensException(“Geen correct rijbewijs”); } else { this.bestuurder = bestuurder; Ingezetenen.add(bestuurder); } Mens[] a […]

扩展java.util.logging.Logger无法正常工作

我想扩展java.util.logging.Logger类。 我有以下代码。 我在我们的应用程序中“强制出错”并且我创建的类没有被调用。 这是我故意生成的错误,但它不在我写的代码中: //Generate error to test the logger String[] myStringArray = new String[3]; String test; test = myStringArray[5]; 我也试过这个,但即使我有相同的函数定义,它仍然没有进入我的代码: logger.log(Level.FINE, “test my logger”); 这是我的记录器扩展。 当“未提出”exception被“引发”时,是否可能触发此操作。 import java.util.logging.Level; import java.util.logging.LogRecord; public class MyLogger extends java.util.logging.Logger { public MyLogger(String name, String resourceBundleName) { super(name, resourceBundleName); runMyLog(); } public void log(Level level, String msg) { runMyLog(); […]

多捕获块中的引用类型的exception问题

e的类型为Exception,但在下面的代码中打印Exception1: class Exception1 extends IOException {void info(){}} class Exception2 extends Exception {} class TestMultiCatch { public static void main(String args[]) { try { int a = 10; if (a <= 10) throw new Exception1(); else throw new Exception2(); } catch (Exception1 | Exception2 e) { e.info(); //line 1 error "The method info() is undefined for […]

如何在java中继续执行try catch语句

可能重复: Java:Try-Catch-Continue? 我正在从文本文件中读取信息,如果读取的类型无效,我希望我的代码抛出exception。 但是,在找到exception后,我无法弄清楚如何继续我的程序 while(input.hasNext()) { try{ type = input.next(); name = input.next(); year = input.nextInt(); } catch(InputMismatchException ex) { //System.out.println(“** Error: Invalid input **”); //code to make program continue } }

捕获exception后for循环继续出现问题

嗨,我是java的半新人,不能想出这个。 捕获exception后,我想要一个for循环继续并继续读取新的整数。 这是一个在线挑战,希望你拿5(这说明它之后应该有多少输入), -150, 150000, 1500000000, 213333333333333333333333333333333333, -100000000000000. 并转入此输出: -150 can be fitted in: * short * int * long 150000 can be fitted in: * int * long 1500000000 can be fitted in: * int * long 213333333333333333333333333333333333 can’t be fitted anywhere. -100000000000000 can be fitted in: * long 我希望计算机检查一个数字对于byte,short,int和long是否不大。 它工作(可能不是最好的方式)直到它达到213333333333333333333333333333333333。它导致InputMismatchException(bc它变大)并且代码捕获它但是在它不起作用之后。 这是输出: -150 can […]