Tag: 例外

exception处理,catch循环停止

我有一个我需要读取的文件,打印出整数,捕获exception并继续显示下一个整数,依此类推,直到没有更多的整数。 该文件包含:12 5 sd 67 4 cy 我希望它显示: 12 五 输入错误 67 4 输入错误 但是,它只给我12,5,然后输入错误,它停止。 我已经尝试将所有内容放入while循环中,并且无限循环地输入exception。 public static void readNumbers() { File inputFile = new File (“C:/users/AC/Desktop/input.txt”); try { Scanner reader = new Scanner(inputFile); while(reader.hasNext()) { int num = reader.nextInt(); System.out.println(“Number read: ” +num); } } catch (InputMismatchException e) { System.out.println(“Input error “); } catch […]

为什么我得到java.util.ConcurrentModificationException?

当我运行以下代码时: import java.util.LinkedList; class Tester { public static void main(String args[]) { LinkedList list = new LinkedList(); list.add(new String(“suhail”)); list.add(new String(“gupta”)); list.add(new String(“ghazal”)); list.add(new String(“poetry”)); list.add(new String(“music”)); list.add(new String(“art”)); try { for(String s : list) { list.add(0,”art”); list.remove(6); System.out.println(list); } }catch(Exception exc) { exc.printStackTrace(); } } } 我得到一个例外,说: java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(Unknown Source) at java.util.LinkedList$ListItr.next(Unknown Source) […]

Java Scannerexception处理

我想从用户那里收到一个Double,并处理在用户没有输入double / int的情况下引发的exception; 在这种情况下,我想要求用户再次插入金额。 如果捕获到exception,我的代码会陷入循环,并继续打印“插入金额”。 private static double inputAmount() { Scanner input = new Scanner(System.in); while (true) { System.out.println(“Insert amount:”); try { double amount = input.nextDouble(); return amount; }catch (java.util.InputMismatchException e) {continue;} } } 先谢谢你。

如何防止InputStream.readObject()抛出EOFException?

我序列化一个对象并将其保存为我的硬盘上的文件。 当我正在阅读它时,仅在某些情况下它会抛出EOFException 。 经过几个小时的调试后,我无法找到问题。 这是我的代码: public void serialize(MyClass myClass,String path) { FileOutputStream foStream = null; ObjectOutputStream ooStream = null; try { File file = new File(path); if (!file.exists()) { file.createNewFile(); } foStream = new FileOutputStream(file); ooStream = new ObjectOutputStream(foStream); ooStream.writeObject(myClass); } catch (Throwable t) { log.error(t); } finally { if (ooStream != null) { try […]

Java风格:正确处理exception

从概念上讲,我一直在为我的项目决定一个exception处理结构。 假设你有一个例子: public abstract class Data { public abstract String read(); } 还有两个子类FileData,它从一些指定文件中读取数据,而StaticData,它只返回一些预定义的常量数据。 现在,在读取文件时,可能会在FileData中抛出IOException,但StaticData将永远不会抛出。 大多数样式指南建议将Exception传播到调用堆栈,直到有足够的上下文可用于有效处理它。 但我真的不想在抽象的read()方法中添加throws子句。 为什么? 因为Data和使用它的复杂机器对文件一无所知,所以它只知道数据。 此外,可能还有其他数据子类(以及更多这些子类)从不抛出exception并完美地传递数据。 另一方面,IOException是必要的,因为如果磁盘不可读(或某些这样的磁盘),则必须抛出错误。 所以我看到的唯一出路就是捕获IOException并在其位置抛出一些RuntimeException。 这是正确的哲学吗?

JavaFX – Application start方法中的exception?

我正在尝试创建一个JavaFX程序,每次我尝试运行我的代码时都会遇到exception – 我不完全确定它的意思是什么…… 我的代码: package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; public class Main extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource(“DogFinderFXML.fxml”)); Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle(“HelloSwingNode Sample”); stage.show(); } /** * @param args the command line arguments */ […]

在Java中,捕获genericsexception和特定exception(例如IOException?)之间的区别是什么?

目前我只捕获通用exception,但我希望更改此function以捕获特定exception,但这有什么优势?

java.util.ConcurrentModificationException在预期时不会抛出

以下代码抛出java.util.ConcurrentModificationException ,如预期的那样: public void test(){ ArrayList myList = new ArrayList(); myList.add(“String 1”); myList.add(“String 2”); myList.add(“String 3”); myList.add(“String 4”); myList.add(“String 5”); for(String s : myList){ if (s.equals(“String 2”)){ myList.remove(s); } } } 但是,以下代码不会抛出exception,而我希望抛出它: public void test(){ ArrayList myList = new ArrayList(); myList.add(“String 1”); myList.add(“String 2”); myList.add(“String 3”); for(String s : myList){ if (s.equals(“String 2”)){ myList.remove(s); } […]

exception永远不会在相应的try语句的主体中抛出

我在Java中遇到exception处理问题,这是我的代码。 当我尝试运行此行时出现编译器错误: throw new MojException(“Bledne dane”); 。 错误是: exceptionMojException永远不会在相应的try语句的主体中抛出 这是代码: public class Test { public static void main(String[] args) throws MojException { // TODO Auto-generated method stub for(int i=1;i<args.length;i++){ try{ Integer.parseInt(args[i-1]); } catch(MojException e){ throw new MojException("Bledne dane"); } try{ WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args[0])); System.out.println(args[i]+" : "+a.wspolczynnik(Integer.parseInt(args[i]))); } catch(MojException e){ throw new MojException(args[i]+" "+e.getMessage()); […]

无法在spring自动assembly油田。 为什么?

我一直得到这个错误,并且无法弄清楚为什么……是的我知道很多人都有类似的问题,但是阅读他们得到的答案并没有解决我的问题。 org.springframework.beans.factory.BeanCreationException:创建名为’contactController’的bean时出错:注入自动连接的依赖项失败; 嵌套exception是org.springframework.beans.factory.BeanCreationException:无法自动assembly字段:private net.service.ContactService net.controller.ContactController.contactService; 嵌套exception是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[net.service.ContactService]的匹配bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 这是控制器: @Controller @SessionAttributes public class ContactController { @Autowired private ContactService contactService; //methods… } ContactServiceImpl @Service(“contactService”) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public class ContactServiceImpl implements ContactService { @Autowired private ContactDao contactDao; public ContactServiceImpl() { } @Override @Transactional(propagation = Propagation.REQUIRED, readOnly = false) public void addContact(Contact […]