throw 关键字用于显式地抛出异常。示例代码public class Test { public static void main(String[] args) { throw new NullPointerException(); } }异常信息Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)throws 关键字在 Java 中用于推迟检查异常的处理。public class Test { public static void main(String[] args)throws NullPointerException { throw new NullPointerException(); } }
当您将格式不正确的字符串变量转换为不兼容的整数(数字格式)时,就会发生此异常。示例代码public class Test { public static void main(String[] args) { int data = Integer.parseInt("hello"); } } 输出结果Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at a6.dateAndTime.Test.main(Test.java:5)