Tag: exception

e.printStackTrace和System.out.println(e)之间的区别

可能是一个新手问题,但每个人似乎都使用e.printStackTrace() ,但我在exception处理时总是使用System.out.println(e) 。 有什么区别,为什么e.printStackTrace()更好?

在finally块中为null赋值null

以下代码的输出是“Test Passed”; 谁能解释一下为什么? public class Test { public static void main(String args[]) { System.out.println(new Test().print()); } protected StringBuilder print() { StringBuilder builder = new StringBuilder(); try { builder.append(“Test “); return builder.append(“Passed!!!”); } finally { builder = null; } }

每个例外都有必要的试用吗?

一个简单的问题,我找不到答案。 Java中的每个exception都需要使用try-catch吗? 或者仅适用于FileNotFoundException? 很多exception(IndexOutOfBoundException,ArithmeticException,IlligalArgumentException,NullPointerException)都表示他们不需要Exception,但是FileNotFoundException确实如此)……我找不到答案,哪些不需要try-catch 。

类抛出exception

我试图在Netbeans中重构一个大型程序,我有点迷失。 我从来没有非常模块化,但我现在正试图纠正这个问题,并且通过实际学习如何做到这一点来实现未来。 不幸的是,我无法将一些教程翻译成我的程序野兽。 所以我希望有人可以提供帮助。 目前,我试图打破一大块代码,这些代码采用特定格式的文件并制作表格。 我知道我需要创建一个类并使用它来创建一个表对象,但我不确定如何。 我有一个main,它获取文件所在位置的命令行输入: public class Print { public static void main(String[] args) throws Exception { // I know this part works JSAP jsap = new JSAP(); FlaggedOption opt3 = new FlaggedOption(“cllmap”) .setRequired(true) .setShortFlag(‘c’) .setLongFlag(“call map”); opt3.setHelp(“Where is the flu, map file? Full path”); jsap.registerParameter(opt3); String cllmp = config.getString(“map”); 我还没有尝试引用我制作的任何新类,因为我还没有弄清楚如何正确地做到这一点。 然后我尝试将文件的位置发送到另一个类,以便另一个类可以读入该文件并将其解析为一个表对象。 public […]

如果服务器返回错误状态,java.net.HttpUrlConnection是否总是抛出IOException?

我正在使用java.net.HttpUrlConnection向我的服务器发出Http请求。 我意识到如果服务器返回错误状态(例如400)。 HttpUrlConnection将抛出与错误状态对应的IOException。 我的问题是: 如果服务器返回错误状态(4xx,5xx),HttpUrlConnection是否总是抛出IOException? 我看一下HttpUrlConnection API描述,但我无法回答我的问题。 更新:请参阅我的测试: public static void main(String[] args) { try { String url = “https://www.googleapis.com/oauth2/v3/userinfo?access_token=___fake_token”; HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection(); conn.getResponseCode(); conn.getInputStream(); } catch (Exception ex) { ex.printStackTrace(); // Print IOException: java.io.IOException: Server returned HTTP response code: 401 for URL: https://www.googleapis.com/oauth2/v3/userinfo?access_token=abc // If I commented conn.getResponseCode() -> The same IOException […]

为什么我得到NoClassDefFoundError:java / awt / Desktop?

我正在尝试用Swing打开一个URI ,我得到了错误。 是什么原因,我该如何解决? 当我在控制台中执行它时一切正常但是当我在GUI中执行时我得到此错误。 我应该说我使用Weblogic作为服务器。 码 private static void open(URI uri) { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(uri); } catch (IOException e) { /* TODO: error handling */ } } else { /* TODO: error handling */ } } 堆栈跟踪: Exception in thread “AWT-EventQueue-1” java.lang.NoClassDefFoundError: java/awt/Desktop at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.open(BcfiDownloadPanel.java:230) at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.access$000(BcfiDownloadPanel.java:37) at be.azvub.ext.bcfidownloder.BcfiDownloadPanel$7.actionPerformed(BcfiDownloadPanel.java:147) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at […]

exception处理,创建日志并在JAVA中继续程序

我正在JAVA中设计一个程序,它可以捕获大约10次迭代的结果。 在这些迭代结束时,必须将所有结果写入日志文件。 如果发生任何exception,那么它应该写在我的文本文件上,其次程序不能停止,它必须继续,直到最后一次迭代完成… 也就是说 – 如果在任何迭代的任何部分发生某些错误, 程序就不能在这里停止 。 必须在我的结果中通过错误名称提及错误,并且必须继续并更新我的日志文件。 我的代码到现在有点冗长…使用try-catch,try块正在进行我的计算并编写我的文本文件,但我需要如果发生一些exception,我的程序一定不能停止,并且必须在我的日志中更新该exception文件。

java.util.ConcurrentModificationException&iteration?

我是Arraylists和迭代器的新手,这是我第一次遇到这个例外。 我有一个ArrayList你想我做以下算法: for (Character c:u){ if(k==1){ //base case if(isAnswer(s+u.get(0))) System.out.println(s+u.get(0)+” is the correct sequence.”+ ‘\n’); return; } else{ u.remove(c); puzzleSolve(k-1, s+c , u); u.add(c); removeLastChar(s); } } //end of for each 当我搜索这个exception时,我发现我无法删除每个arrayl上的iterms weth并且我需要使用迭代器,但是我很困惑我在哪里以及如何确定我必须放入while(iter.hasNext())这段代码就是这样的东西。 如果你能帮助我,我将不胜感激 PS。 s是String(最初为空)&k是int

在程序中获得意外的输出

这是代码: String sql_1 = “select emp_id,password from regid”; ResultSet rs = st.executeQuery(sql_1); while(rs.next()) { if(((employee.equals(rs.getString(“emp_id”))) && (password.equals(rs.getString(“password”))))==true) { // String sql2=”update regid set regid='”+Datastore.regIds.add(regId)+”‘ where emp_id='”+employee+”‘”; // st.executeUpdate(sql2); System.out.println(“2> Employee Id : “+employee+” && Password : “+password); System.out.println(“3> This employee “+employee+” exsists in the database and registration-password id will be Updated”); // resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(“text/html”); PrintWriter […]

无法弄清楚如何捕获InputMismatchException

所以这是我当前捕获InputMismatchException错误的代码 int weapon = 0 boolean selection = true; while(selection) { try { System.out.println(“Pick number 1, 2, or 3.”); weapon = scan.nextInt(); selection = false; } catch(InputMismatchException e) { System.out.println(“Choose 1,2,3”); weapon = scan.nextInt(); } } 我正在尝试确保输入int而不是其他任何东西。 扫描仪类已经实现,“扫描”将对我起作用。 感谢您的帮助!