Tag: 例外

最终吞噬了例外

static int retIntExc() throws Exception{ int result = 1; try { result = 2; throw new IOException(“Exception rised.”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); result = 3; } finally { return result; } } 我的一个朋友是.NET开发人员,目前正在迁移到Java,他问我关于这个来源的以下问题。 理论上,这必须throw IOException(“Exception rised.”) retIntExc() throw IOException(“Exception rised.”)并且整个方法retIntExc()必须throws Exception 。 但没有任何反应,该方法返回2。 我没有测试他的例子,但我认为这不是预期的行为。 编辑:谢谢你的所有答案。 有些人忽略了这个方法被称为retIntExc的事实,这意味着这只是一些测试/实验示例,显示了抛出/捕捉机制中的问题。 我不需要’修复’,我需要解释为什么会这样。

正在使用的远程方法调用端口

我用RMI创建了一个Server,Client类程序。 但是每当我从命令提示符启动rmiregistry后运行我的服务器时,就会抛出已经处于使用状态的端口错误。 只有我开始了这个体验。 我从netstat检查了它。 服务器代码: public class Server implements Runnable, Linker{ private static Server server = null; private static Linker l = null; private String name = null; public Server(){} public void setName(String name){ this.name = name; } public String getName(){ return name; } public void run(){ while(!(“Andy”).equalsIgnoreCase(name)){ } } public static void createStub(){ try{ […]

使用try / catch时无休止的循环问题

我有一个while循环,它应该捕获一个非int输入并要求用户重新输入一个int。 然而,它只是无休止地循环错误消息。 有没有人知道为什么第二次不允许扫描仪输入? while(!enteredInt) { try { locus = input.nextInt(); enteredInt = true; } catch (Exception e) { //user didn’t type an integer System.out.println(“You didn’t enter a valid number, re-enter point where you will build your city”); } }

在java中扩展Exception / RunTimeException?

我有以下课程。 public class ValidationException extends RuntimeException { } 和 public class ValidationException extends Exception { } 我很困惑,自定义exception何时应该扩展RunTimeException ,何时必须扩展Exception 。 你能解释一下我是否有直接扩展RunTimeException缺点? 谢谢!

为什么Java在这里抛出NullPointerException?

public class Test { public int [] x; public Test(int N) { int[] x = new int [N]; for (int i=0;i<x.length;i++) { x[i]=i; StdOut.println(x[i]); } } public static void main(String[] args) { String path = "/Users/alekscooper/Desktop/test.txt"; In reader = new In(path); int size=reader.readInt(); StdOut.println("Size = "+size); Test N = new Test(size); StdOut.println(Nx[3]); } /* ADD […]

为什么我们不必将run-catch添加到RuntimeException?

我想问为什么我们不必将run-catch块添加到RuntimeException而我们应该使用其他exception? 我的意思是: public class Main { public static void main(String[] args) { throw new RuntimeException(); } } 编辑:当我说: throw new RuntimeException(); 很明显会发生exception,为什么编译器不禁止这样做呢?

在Java中检查vs未经检查的exception

我在理解Java中已checked和uncheckedexception之间的差异时遇到了一些问题。 首先, checkedexception应该在编译期间查找exception。 在不同来源中提供的示例引用数据库连接,文件处理作为其中一些,而uncheckedexception应该在程序员的部分寻找错误,例如超出数组范围的索引等。 不应该反过来吗? 我的意思是,数据库连接是在运行时完成的,对吧? 文件处理也是如此。 您在编译期间没有打开文件句柄,那么为什么在编译期间会查找可能的错误? 另一方面,在程序中已经完成了索引超出其范围的数组,可以在编译期间检查(如果用户在运行时提供exception索引,那么它可以是运行时)问题)。 我在这里想念的是什么? 2其次, RunTimeException本身如何被unchecked ,子类Exception ,哪个被checked ? 这意味着什么? 我在Herbert Schildt的书中找到了一个例子,解释了checkedexception的用法: class ThrowsDemo { public static char prompt(String str) throws java.io.IOException { System.out.print(str + “: “); return (char) System.in.read(); } public static void main(String args[]) { char ch; try { ch = prompt(“Enter a letter”); } catch(java.io.IOException exc) […]

Java中exception的throws关键字

当你这样做: public class Blah { public void doBlah() throws BlahException { } } 添加throws BlahException真的有什么作用? 它基本上将任何exception分组到那个吗? 即如果有exception,无论它是什么,将始终使用BlahException抛出?

如何将printStackTrace()中的exception写入Java中的文本文件?

我需要在Java中的文本文件中捕获exception。 例如: try { File f = new File(“”); } catch(FileNotFoundException f) { f.printStackTrace(); // instead of printing into console it should write into a text file writePrintStackTrace(f.getMessage()); // this is my own method where I store f.getMessage() into a text file. } 使用getMessage()有效,但它只显示错误消息。 我想要printStackTrace()中的所有信息,包括行号。

HTTPS主机名错误:应为。 是什么导致这个?

尝试使用https连接到服务器时,我收到此’HTTPS hostname wrong:’错误。 我的url看起来像这样 https://sub.domain.com/tamnode/webapps/app/servlet. 我使用以下代码连接 // Create a URLConnection object for a URL URL url = new URL(requestedURL); HttpURLConnection.setFollowRedirects(false); // connect connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty(“User-Agent”, USER_AGENT); //$NON-NLS-1$ OutputStreamWriter wr = new OutputStreamWriter(connection .getOutputStream()); 但后来得到一个错误 IOException: HTTPS hostname wrong: should be . at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing …. 这是过去曾经工作但不再有效的代码。 系统架构已经发生了一些变化,但我需要在接近负责人之前获得更多数据。 什么可能导致此错误? 我可以关闭URLSpoofing检查吗?