【正文】
s if the file can39。 read the file into memory。 } catch (memoryAllocationFailed) { doSomething。 } method2 { call method3。 if (error) doErrorProcessing。 if (error) return error。 if (error) return error。 } } method2 throws exception { call method3。 allocate that much memory。 determine its size。 } catch (fileOpenFailed) { doSomething。 } catch (fileCloseFailed) { doSomething。 c = (char)()。 } } java jex7_9 Exception in thread main“ : / by zero class jex7_9 { public static void main(String args[]) { int i = (args[0])。 (c)。 } catch (IOException e) { (e)。 class Test { public static void main(String args[]) { try { char c = (char)()。 } catch (IOException e) { (e)。 } public static void main(String args[]) { try { char c = getChar()。 … …。 try { ()。 } ? 自定義異常類(lèi)定義自已的構(gòu)造方法 異常 (Exception) 35 ? 自定義異常 ? 自定義異常類(lèi)定義自已的構(gòu)造方法 異常 (Exception) class MyException extends Exception { private int x。 } } class ExtraFeatures { public static void f() throws MyException { (Throws MyException from f())。 } public static void h() throws MyException { (Throws MyException from h())。 } Throwing MyException from f() MyException g()。 therefore, it catches any exception. This can be a poor implementation because you are losing valuable information about the type of exception being thrown and making your code less efficient. As a result, the runtime system is forced to determine the type of exception before it can decide on the best recovery strategy. 38 ? Quiz ? Question: What exception types can be caught by the following handler? 異常 (Exception) ... } catch (Exception e) { ... } catch (ArithmeticException a) { ... } ? Answer: This first handler catches exceptions of type Exception。36 ? Quiz ? Question: Is the following code legal? 異常 (Exception) try { ... } finally { ... } ? Answer: Yes, it39。 } } try { f()。 } public static void g() throws MyException { (Throws MyException from g())。 } public MyException(String msg, int x) { super(msg)。 (Caught it!)。 if (…) throw new SimpleException()。 } catch (IOException e) { (e)。 class Test { static char getChar() throws IOException { char c = (cha