Tag: exception

为什么Java编译器允许在throws部分中列出exception,该方法无法抛出exception

如果有一些代码显然无法抛出exception,那么Java编译器似乎不一致,并且您编写了声明代码可以抛出该exception的周围代码。 请考虑这些代码段。 片段1 从未抛出的exceptioncatch 。 public void g(){ try { } catch (FileNotFoundException e) {//any checked exception } } 消息是编译错误 Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body Snippet2 throws声明,指示从不抛出的exception。 public void g() throws FileNotFoundException{ } 它汇编很好。 因此,第一个代码段的结果显示编译器可以计算方法是否可以抛出throws列表中列出的throws 。 因此,似乎编译器故意不报告第二个片段的错误。 但为什么? 为什么编译器允许您在throws部分编写exception,即使编译器知道这些exception不能被抛出?

PKIX路径构建在Java应用程序中失败

在将我的应用程序从Windows 2000移动到Windows 2008 R2 Server之后,我一直在努力让我的应用程序运行近一周。 步骤: 已安装Java JDK 1.7.0_25 将系统环境变量JAVA_HOME为C:\Progra~1\Java\jdk1.7.0_25\ 使用keytool将证书导入cacerts 确保证书存在于keytool with -list 。 我试图用InstallCert重复第3步 ,以确保我没有弄乱任何东西。 上面的方法没有解决我的问题,所以我尝试以编程方式进行: System.setProperty(“javax.net.ssl.trustStore”, “C:/Progra~1/Java/jdk1.7.0_25/jre/lib/security/cacerts”); System.setProperty(“javax.net.ssl.trustStorePassword”, “changeit”); 仍然没有任何运气。 我被卡住了,不太确定从这里走哪个方向。 堆栈跟踪: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868) […]

Java Oracle localhost连接错误(ORA-12505)

我正在尝试连接到当前计算机上的数据库。 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { public static void main(String[] argv) throws Exception { Connection connection = null; try { // Load the JDBC driver String driverName = “oracle.jdbc.driver.OracleDriver”; Class.forName(driverName); // Create a connection to the database String serverName = “localhost”; String portNumber = “1521”; String sid = “xe”; String […]

将已检查的exception包装到Java中未经检查的exception中?

我在java中有这个工厂方法: public static Properties getConfigFactory() throws ClassNotFoundException, IOException { if (config == null) { InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP); config = new Properties(); config.load(in); } return config; } 我想将两个已检查的exception转换为未经检查的exception。 最好的方法是什么? 我应该捕获exception并使用捕获的exception作为内部exception抛出一个新的RuntimeException吗? 有没有更好的方法来做到这一点,或者我是否应该首先尝试这样做? 编辑: 只是为了澄清。 这些exception将是致命的,因为配置文件基本上是程序的操作,所有exception都将被捕获并记录在我的程序的顶层。 我的目的是避免不必要的抛出exception,将exception添加到调用我的工厂的每个方法的签名中。

空结果集上的非法操作

我正试图在杂货店建立一个付款台,我的代码实际上执行了我打算做的事情,但有一件事。 在我要求用户输入他们想要的项目数量之后,产品信息被收集并且工作正常,但是当它要求用户输入下一个产品的产品ID时,该行重复,我得到以下内容我的捕获中的exception:“空结果集上的非法操作”。 同样,所有的计算和一切都很好,除了重复那一行。 关于可能出现什么问题的任何想法? 重复的输出是这样的: 输入产品(或退出): 错误1:空结果集上的非法操作。 输入产品(或退出): 这是代码。 try { Class.forName(“com.mysql.jdbc.Driver”); String connection = “jdbc:mysql://myDB?”; connection = connection + “user=xxx&password=xxxxxx”; Connection conn = DriverManager.getConnection(connection); // MATA IN PRODUKTNUMMER System.out.println(“\nEnter product (or Exit):”); GroceryStore.input = GroceryStore.scan.nextLine(); PreparedStatement stmt = conn.prepareStatement( “SELECT * “+ “FROM Products “+ “WHERE productNo = ?”); stmt.setString(1, GroceryStore.input); ResultSet rs = […]

何时在Java中使用exception(示例)

我知道这会是不好的做法,虽然我知道我无法解释原因。 int [] intArr = … … try{ int i = 0; while(true){ System.out.println(intArr[i++]); } }catch(ArrayIndexOutOfBoundsException e){} 我认为你只应该对不应该发生的事情使用例外。 我问这个问题是因为我认为我有时会使用exception错误。 如果你的编程运行一个标准的情况应该抛出exception? 这似乎是相关的: 防止exception与捕获Java中的exception

MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException

我正在做一个java项目,我遇到了这个问题,不知道如何解决它。 我项目中的类(简化): public class Item { private String itemID; private Integer price; public Integer getPrice() { return this.price; } } public class Store { private String storeID; private String address; } public class Stock { private Item item; private Store store; private Integer itemCount; public Integer getInventoryValue() { return this.item.getPrice() * this.itemCount; } } 然后我尝试排序Stock的ArrayList […]

JavaFx图像路径

我的Java类有问题。 实际上代码是正确的,但如果我单击运行按钮,则会导致图像路径exception。 static Image currentBackground = new Image(“Snake/Images/background_options.png”, true); 编译器的消息是: Exception in thread “main” java.lang.ExceptionInInitializerError at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122) Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1100) at javafx.scene.image.Image.(Image.java:624) at view.OptionsWindow.(OptionsWindow.java:21) … 3 more Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1092) … 5 […]

在try \ finally块中是否存在这样的情况,finally将不会被执行?

我正在攻读面向对象编程的测试,我想知道是否有任何案例,考虑以下代码: try { do something } catch (someException e) { } finally { do something } finally块不会执行?

在预期时处理NumberFormatException的正确方法是什么?

我遇到了这种情况,我需要将一个String解析为一个int ,我不知道如何处理NumberFormatException 。 当我没有抓住它时,编译器不会抱怨,但我只是想确保我正确处理这种情况。 private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; } return i; } 我想简化这样的代码。 编译器没有问题,但线程在NumberFormatException上死亡。 private int getCurrentPieceAsInt() { int i = 0; i = Integer.parseInt(this.getCurrentPiece()); return i; } Google CodePro希望我以某种方式记录exception,我同意这是最佳做法。 private int getCurrentPieceAsInt() { int i = 0; […]