Java try/catch 异常处理的问题

2019-03-25 13:32|来源: 网路

public static void main(String[] args) {
		System.out.println(test(null)+","+test("")+","+test("1"));
	}

	@SuppressWarnings("finally")
	private static int test(String str) {
		try{
			return str.charAt(0)-'0';
		}catch(NullPointerException e){
			e.printStackTrace();
			return 1;
		}catch(RuntimeException e){
			e.printStackTrace();
			return 2;
		}catch(Exception e){
			e.printStackTrace();
			return 3;
		}finally{//finally  永远被执行
			return 4;
		}
			
	}
 

求输出结果及解释,本人菜鸟,见笑了!

 

相关问答

更多
  • 区别就是,第一种是系统直接抛出异常了,程序在出错的地方就终止了。第二种,你自己捕获了异常,程序执行完catch()里面的代码后还可以继续执行后续的代码。 你这里的例子不是很明显,因为try,catch()的作用并不是用来打印出错信息的。而是保证程序不会因为出异常而中止执行。
  • 如果try块中的任何代码可以抛出一个被检查的异常,它必须出现在方法签名的throws子句中。 如果抛出未检查的异常,它将冒出来。 始终执行finally块,无论是否抛出异常。 If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, i ...
  • 这是可能的, 因为Java 7 。 try-catch块的语法是: try { ... } catch (IOException | SQLException ex) { ... } 在Java 7之前,这是不可能的。 记住,如果所有异常都属于同一个类层次结构,则可以简单地捕获该基类异常类型。 唯一的方法是在自己的catch块中捕获每个异常。 编辑:请注意,在Java 7中,如果ExceptionB直接或间接从ExceptionA继承,则不能捕获同一块中的ExceptionA和Exceptio ...
  • is.read()声明它抛出IOException 。 InvalidClassException扩展IOException 。 因此,就您的代码所知, is.read()可能会抛出InvalidClassException 。 所以你可以尝试抓住它。 is.read() declares that it throws IOException. InvalidClassException extends IOException. Therefore as far as your code knows, it ...
  • 捕获泛型异常还将捕获NullPointerException,IndexOutOfBoundsException以及您通常不想捕获的其他异常。 通过捕获您想要处理的特定异常,您可以避免这些情况。 Catching generic Exception will also catch NullPointerException, IndexOutOfBoundsException and other exceptions that you generally don't want to catch. By cat ...
  • 为了更好地了解正在发生的事情,请查看Java异常层次结构。 在该图中可以看到广泛的概述。 当您在单个catch块中捕获多个异常时,您应该只使用catch中的“最顶层”异常。 例如,您的错误将持续存在 catch (IOException | FileNotFoundException e) 因为FileNotFoundException扩展了IOException 。 在您的代码中,所有异常( OutOfMemoryError除外)都是java.lang.Exception子类型,因此是错误。 OutOf ...
  • 在您提供的方法中,无法捕获异常,因为简单赋值不会抛出任何异常。 您所能做的就是以下变化: if(date == null) return false; 但即便如此也不优雅。 您可能希望使用this.date执行某些操作,或者如果这是所需的行为则抛出异常。 你真正想要的是: ParseException - 由DateFormat对象尝试parse()时抛出,这将在set方法之前发生 IllegalArgumentException - 由SimpleDateFormat构造函数抛出,同样会在set方法之前 ...
  • 有两个选项可用1.抛出异常 - >这意味着如果发生异常情况,程序会要求调用者处理它。 2.尝试/ catch - >这意味着方法本身将处理它。 差异是微妙的,是的,有时我们会抓住我们在下一层抛出的异常。 但如果仔细考虑这些陈述,那就是两种方法之间的核心差异。 在大型团队中,我们经常有子团队在层上工作。 内层可能只是选择抛出异常并停止进一步执行流程。 外层应该捕获它并显示优雅的错误消息或优雅的方式来处理这些异常,以便用户不会看到Java堆栈跟踪。 希望这可以帮助。 Two options are availa ...
  • 尝试这个 public static void main(String[] args) { double wage = Validate.collectWage("Please enter your hourly wage"); }// end main method public static Double collectWage(String messageIn) { Scanner input = new Scanner(System.in); String strOut ...
  • 您应该能够设置Selenium的日志首选项来自定义异常输出。 编辑:也许上面引用的链接中的说明不太有用。 看起来Selenium使用标准的Java记录器接口。 获取RemoteWebDriver对象,然后通过setLogLevel(java.util.logging.Level.OFF)关闭其日志记录。 另一种选择, FirefoxDriver允许将stdout / stderr重定向到文件。 这样做的好处是可以在需要时检查输出; 否则,您可以覆盖它或删除它。 You should be able to s ...