异常处理机制 readFile open the file determine its size allocate the much memory read the file into memory close the file 3 catch(fileOpen Failed)t handl 1 catch (sizedetermination Failed)tt handle 3 catch(memory Allocation Failed)t handle: 3 catch (readFailed) handle 3 catch(fileCloseFailed)t handle
异常处理机制 readFile{ try{ open the file; determine its size; allocate the much memory; read the file into memory; close the file; } catch(fileOpenFailed){ handle; } catch(sizedeterminationFailed){ handle; } catch(memoryAllocationFailed){ handle; } catch(readFailed){ handle; } catch(fileCloseFailed){ handle; } }
Catch runtime errors import javax. swing. JOptionPane public class Test t public static void main(string[] args)i String input JOptionPane showInputDialog(null, "Please enter an integer") 如果这行出现异常,就会跳过try子句中其余 int number Integer parseInt(input) 的行,并把程序控制转移到 catch子句 // Display the result JOptionPane showMessageDialog(null ii The number entered is number catch( Excepti。nex) JOptionPane. showMes sageDialog(null iNcorrect input: an integer is required")i 捕获并处理这个异常后,程序控制转移到 Iry- catch块之后的第一条语句 System. out. println ("Execution continues ...")i System. exit(o)
Catch Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { try { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Incorrect input: an integer is required"); } System.out.println("Execution continues ..."); System.exit(0); } } 如果这行出现异常,就会跳过try子句中其余 的行,并把程序控制转移到catch子句 捕获并处理这个异常后,程序控制转移到 Try-catch块之后的第一条语句
EXception Classes ClassNot Found Exception IOException ArithmeticException AWTException NullPointer Except RuntimeExe IndexOutOfBounds Exception Throuble Several more classes galArgumentException Linkage error Several more classes VirtualMachine Error Erro AWTError
Exception Classes LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException
System Errors ClassNot Found Exception IOExcepti Arithmetic Exception Exception AWTException lullPointer Excepti RuntimeException IndexOutofBounds Exception Object Throwal Several more classes IllegalArgument Exception System errors是由Java虚 Several more classes 拟机抛出并在Eror类中描 LinkageError 述的。Eror类描述内部的 VirtualMachine Error 系统错误。这种错误很少 Error 发生,一旦发生,除了通 AWTErro 知用户以及尽量稳妥地结 束程序外,几乎什么也不 Several more classes 能做
System Errors LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException System errors是由Java虚 拟机抛出并在Error类中描 述的。Error类描述内部的 系统错误。这种错误很少 发生,一旦发生,除了通 知用户以及尽量稳妥地结 束程序外,几乎什么也不 能做