Tag: 例外

为什么不打印此例外? 为什么会出现错误?

如果我试图打印“a”的值,为什么显示错误? 为什么exception会成为错误? class Ankit1 { public static void main(String args[]) { float d,a; try { d=0; a=44/d; System.out.print(“Its not gonna printed”+a); //if Exception not occurs then it will print and it will ot goto catch block } catch(ArithmeticException e) { System.out.println(“Print hoga”+a);//why error come?? } } }

junitparameterexception方法应该没有参数

通过使用JUnitParameter来测试方法我有一个例外。 我的代码类似于JUnitParameter上的大量示例: private Object parametersForTestSetDistanceFormated() { return new Object[][]{ {100, “_1,__ km”}, {100, “1_,__ km”}, {1100, “11,__ km”}, {110, “1_,1_ km”}, {111, “1_,11 km”}}; } /** * Test of setDistanceFormated method, of class ClientFormated. */ @Test @Parameters public void testSetDistanceFormated(final int exptDist, final String distFormated) { System.out.println(“setDistanceFormated”); ClientFormated instance = new ClientFormated(); instance.setDistanceFormated(distFormated); assertEquals(exptDist, (long) […]

java中的无头exception

我听说当我们使用awt或swing时,在创建FRAME时,可能会抛出一个未经检查的exception,即“无头exception”。 我从来没有得到这个例外。 任何人都可以告诉什么时候抛出这个exception?

修复错误:未报告的exceptionInterruptedException

我是Java的新手。 我只是在搜索如何使Java程序等待,并说它使用Thread.sleep()方法。 但是,当我这样做时,它会出现错误: 错误:未报告的exceptionInterruptedException; 必须被抓住或宣布被抛出 我通过向方法声明添加throws InterruptedException来修复它,现在它可以工作了。 但是,在调用方法时,我再次收到错误。 人们说要使用抛出和捕获块,但我不知道该怎么做。 有人可以帮我吗? 无论如何,Draw.java的代码(使用sleep()方法): package graphics.utilities; public class Draw { public static void DS(int[] c) throws InterruptedException { \\ .. Drawing Algorithms Thread.sleep(2000); \\ .. More Drawing Algorithms } } 在Square.java中(调用DS()): package graphics.shapes; import graphics.utilities.*; public class Square implements Graphics { int x1,y1,s; public Square(int x1,int y1,int s) […]

如何强制Java抛出算术exception?

如何强制Java在除以0.0或从负双精度中提取根时抛出算术exception? 代码如下: double a = 1; // or a = 0 to test division by 0 double b = 2; double c = 100; double d = b*b – 4*a*c; double x1 = (-b – Math.sqrt(d)) / 2 / a; double x2 = (-b + Math.sqrt(d)) / 2 / a;

当main方法抛出exception时,它意味着什么?

我正在审查我为明天上午的期末考试做准备的期中考试。 我错了这个问题,但是没有正确的答案指出,我忽略了向教授询问它。 请考虑以下代码段: public static void main(String[] args) throws FileNotFoundException 关于此代码的以下哪些陈述是正确的? main方法旨在捕获和处理所有类型的exception。 main方法旨在捕获和处理FileNotFoundException 。 如果发生FileNotFoundException ,主要方法应该简单地终止。 如果发生任何exception,主要方法应该简单地终止。 我选择了第二个选项。

java.util.ConcurrentModificationException问题

在这段代码中,我得到一个java.util.ConcurrentModificationException,该方法在webservice中,首先读取文件并检查vakNaam是否在文件中。 然后它将被删除,文件将被重写。 exception2抛出exception(在println中) @WebMethod public boolean removeVak(String naam){ ArrayList tempFile = new ArrayList(); //Read the lines boolean found = false; BufferedReader br = null; try { br = new BufferedReader(new FileReader(“C:/vak.txt”)); String strLine; while ((strLine = br.readLine()) != null){ tempFile.add(strLine); } }catch(Exception e){ System.out.println(“Exception ” + e); }finally { try { if (br != null) […]

java.lang.VerifyError函数调用的不兼容对象参数

在编写一些java代码时,我遇到了一个我无法识别的exceptionjava.lang.VerifyError。 一些谷歌搜索表明这通常是一个jvm / javac错误,我很好奇,如果我的情况是。 我怀疑的是 private Pair<Integer/*used size*/,Pair[]>[] map=(Pair<Integer,Pair[]>[])Array.newInstance(Pair.class,63);//good start number 和 map[b]=new Pair<Integer,Pair[]>(7,(Pair[])Array.newInstance(Pair.class,7)); 但我很不确定。 这是编译器错误还是我的错误代码。 这些行是我在某处找到的generics数组创建失败的解决方法。 附加代码。 package osm2spacebook; import java.util.Iterator; import java.lang.reflect.Array; import java.util.NoSuchElementException; public class MultiMap implements Iterable{ private int num_keys; @SuppressWarnings(“unchecked”) private Pair<Integer/*used size*/,Pair[]>[] map=(Pair<Integer,Pair[]>[])Array.newInstance(Pair.class,63);//good start number @SuppressWarnings(“unchecked”) private int bucket(K key){//position in bucket int h=key.hashCode(); int b=h%map.length; if(map[b]==null) map[b]=new Pair<Integer,Pair[]>(7,(Pair[])Array.newInstance(Pair.class,7)); […]

如何区分Programmer和JVM Exceptions

正如标题所暗示的,我如何从编程方式 (这是否意味着,由程序员或程序抛出 ) 抛出exception来告诉JVM 抛出exception ? JVM例外 1) ArrayIndexOutOfBoundsException 2) ClassCastException 3) NullPointerException 以编程方式抛出 1) NumberFormatException 2) AssertionError 非常感谢

捕获IllegalArgumentException的最佳方法是什么

什么时候才能最好地利用这种类型的例外,如果陷入这样的陷阱,它是否能正常运行? catch(Exception e) 还是需要明确捕获? catch(IllegalArgumentException e)