将序列化对象写入文件后,可以从文件中读取并反序列化它,也就是说,表示对象及其数据的数据类型信息和字节可用于在内存中重新创建对象。示例 import java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); ... 阅读更多
当您将数字除以零时,会抛出 ArithmeticException 异常。示例 实时演示 public class DividedByZero { public static void main(String args[]) { int a, b; try { a = 0; b = 54/a; System.out.println("hello"); } catch (ArithmeticException e) { System.out.println("您不能将数字除以零"); } } }输出您不能将数字除以零
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(); } }