Tag: try catch

在java中尝试catch-finally

在Java中,如果我们在try-catch-finally的try块中插入一个return语句,那么finally块是否会被执行?

试图提示用户从catch块重新进入,但catch块终止了吗?

我正在尝试编写一个程序,要求用户输入他们的年龄并提示他们重新输入,如果他们输入了不正确的值(例如负数,年龄超过120,年龄有特殊字符或字母,范围号等…) 我尝试写一个try / catch来要求用户重新输入他们的年龄: System.out.println(“Enter your age (a positive integer): “); int num; try { num = in.nextInt(); while (num 120) { System.out.println(“Bad age. Re-enter your age (a positive integer): “); num = in.nextInt(); } } catch (InputMismatchException e) { //System.out.println(e); System.out.println(“Bad age. Re-enter your age (a positive integer): “); num = in.nextInt(); } 当输入的年龄包含特殊字符/字母或超出范围时,程序会打印出“Bad […]

如何避免在try语句中设置变量

我的问题是我必须在try语句中设置一个变量,否则我会收到编译错误。 后来我需要使用那个变量,但它现在超出了范围,或者我相信。 我在try语句之外初始化变量并将其设置为null,我认为它可能在外部可访问,但我仍然得到NullPointerException 。 代码在下面,大量的内容使得阅读变得更容易 – 我知道这是不好的代码,但我是Servlets的新手,只是想看到它运行所有运动部件做他们应该做的事情。 我创建了另一个调用createDocs(…)并传入所需参数的类,它工作正常。 所以这让我很好奇为什么当我调用rs.getString(“name”)我得到了NullPointerException ,因为这正是我从其他类做的(为方便起见,从main方法运行)并且它按预期工作。 有问题的变量是ResultSet变量“rs” – public class AgReportServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AgReportServlet() { super(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ResultSet rs = null; try { rs = docs.getDocs(con, start, end, zone, locality); } catch […]

在“while循环”中使用“try and catch”块时无限循环

当我在while循环中使用try和catch块时,我的程序有一个无限循环 。 import java.util.*; class Try { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(true) { try { System.out.println(“Enter a no “); int s=sc.nextInt(); } catch(Exception e) { System.out.println(“Invalid input try again”); } } } } 当我输入一个整数时,它运行正常并要求另一个输入,但是当我输入一个char时,它会进行无限循环。 为什么会这样?

尝试,最后在try块中返回执行流程

尝试时,最后使用组合,如果有返回语句,请尝试。为什么最后是块执行? class Palindrome { public static void main(String args[]) { System.out.println(Palindrome.test()); } public static int test() { try { //return 0; return 100; } finally { System.out.println(“finally trumps return.”); } } } 在上面的代码中,请告诉我执行的流程。 我知道最终将在try块之后强制执行。但是在try块中,返回staatement会将控件带到主类。 在那种情况下,控制将如何最终阻止?

无限循环当在try-catch块中捕获InputMidmatchException时

我一直在无限循环中捕获我的代码。 没什么可提升的,但我无法弄明白我的生活! 有人请帮忙 我只是重新创建了特定的错误,没有我在实际程序中的所有if语句。 package bs; import java.util.InputMismatchException; import java.util.Scanner; public class bs { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean continueVar = true; while (continueVar) { try { System.out.println(“Enter Something”); int input = sc.nextInt(); } catch (InputMismatchException i) { System.out.println(“What the f***?”); continueVar = true; } } } } […]

如何将字符串转换为float并避免在java中使用try / catch?

有一些情况我需要将字符串转换为浮点数或其他一些数值数据类型,但有可能获得一些不可转换的值,如“ – ”或“/”,我无法预先validation所有值以删除他们。 我想避免使用try / catch这个问题,还有其他方法在java中进行正确的转换吗? 类似于C# TryParse东西?

如果运行该函数的线程被中断,finally块是否会执行?

如果我有一个带有try / finally部分的函数,并且运行它的线程在try块中被中断,那么finally块是否会在中断实际发生之前执行?

Java尝试捕获块

这是一个简单的问题: 您如何看待每次使用try catch的代码? void myfunction() { try { instruction1(); } catch (ExceptionType1 e) { // some code } try { instruction2(); } catch (ExceptionType2 e) { // some code } try { instruction3(); } catch (ExceptionType3 e) { // some code } try { instruction4(); } catch (ExceptionType4 e) { // some code } // […]

Java InputMismatchException

我有这个代码,我想捕获字母exception,但它一直有这些错误: Exception in thread “main” java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at exercise_one.Exercise.main(Exercise.java:17) 这是我的代码: System.out.print(“Enter the number of students: “); students = input.nextInt(); while (students <= 0) { try { System.out.print("Enter the number of students: "); students = input.nextInt(); } catch (InputMismatchException e) { System.out.print("Enter the number of students"); } }