Friday, September 3, 2010

Java exception handling of special cases

1, can not finally block the implementation of return, continue such statements, or exceptions will "eat"; 2, in the try, catch the return statement if, in the implementation of the return prior to implementation of the finally block below please example: public class TryTest (public static void ...

1, not in the finally block in the implementation of the return, continue such statements, or exceptions will "eat";
2, in the try, catch the return statement if, in the implementation of the return prior to implementation of the finally block

Please the following example:



public class TryTest (
public static void main (String [] args) (
try (
System.out.println (TryTest.test ());// returned no results for the true anomaly of its

) Catch (Exception e) (System.out.println ("Exception from main");
e.printStackTrace ();
)
doThings (0);
)
public static boolean test () throws Exception (
try (
throw new Exception ("Something error ");// Step 1. throws Exception

) Catch (Exception e) (/ / Step 2. Catch the exception matches (in class or parent class), access control block

System.out.println ("Exception from e ");// Step 3. Print

return false; / / Step 5. return before control is transferred to the finally block, then return to complete the implementation (this step was eaten, not the implementation)

) Finally (
return true; / / Step 4. control transfer, directly back, eat an exception

)
)
public static void doThings (int i)
(
try
(
if (i == 0)
(
/ / Return before the Council in the implementation of the first implementation of the finally

return;
)
int t = 100 / i;
System.out.println (t);
) Catch (Exception ex)
(
ex.printStackTrace ();
)
finally
(
System.out.println ("finally");
)
)
)

No comments:

Post a Comment