Java Exception Handling Summary

Source: Internet
Author: User

1. In Java Exception Handling, an exception is thrown in one domain (11 lines throw an exception and 12 or 13 lines of code are not executed). After catch is captured and processed, the code after the domain continues to be executed (lines 19 and 20). If the catch of the domain contains the finally statement, after the code in the finally (lines 17) is executed, continue to execute the subsequent code (lines 19 and 20 ).

 1 public class ExceptionTest {
2
3
4 class MyException extends Exception{
5
6 }
7
8 String sf1() {
9 int i = 0;
10 try{
11 if(i==0)throw new MyException();
12 System.out.println("IN");
13 return null;
14 }catch(MyException e){
15 System.out.println("CATCH");
16 }finally{
17 System.out.println("FINALLY");
18 }
19 System.out.println("OUT");
20 return null;
21
22 }
23 /**
24 * @param args
25 */
26 public static void main(String[] args){
27 // TODO Auto-generated method stub
28 new ExceptionTest().sf1();
29 }
30
31 }
//console output
CATCH
FINALLY
OUT

2. Even if the function has been returned through return, if the returned statement is surrounded by catch and finally statements, the finally statement content can still be executed (18 rows ).

 1 public class ExceptionTest {
2
3
4 class MyException extends Exception{
5
6 }
7
8 String sf1() {
9 int i = 0;
10 try{
11 if(i!=0)
12 throw new MyException();
13 System.out.println("IN");
14 return null;
15 }catch(MyException e){
16 System.out.println("CATCH");
17 }finally{
18 System.out.println("FINALLY");
19 }
20 System.out.println("OUT");
21 return null;
22
23 }
24 /**
25 * @param args
26 */
27 public static void main(String[] args){
28 // TODO Auto-generated method stub
29 new ExceptionTest().sf1();
30 }
31
32 }
//console output
IN
FINALLY

3,
If a function declares an exception and throws this exception through throw in the function body, and try or catch is not used to capture the exception in the periphery, the function is executed here, but if try or catch exists in the periphery,
Finally statement. The statements in final still need to be executed (14th rows ). However, the Code after the entire domain will not be executed (16th rows ).

 1 public class ExceptionTest {
2
3
4 class MyException extends Exception{
5
6 }
7
8 String sf1() throws MyException {
9 int i = 0;
10 try{
11 if(i==0)throw new MyException();
12 System.out.println("IN");
13 }finally{
14 System.out.println("FINALLY");
15 }
16 System.out.println("OUT");
17 return null;
18
19 }
20 /**
21 * @param args
22 */
23 public static void main(String[] args) throws MyException {
24 // TODO Auto-generated method stub
25 new ExceptionTest().sf1();
26 }
27
28 }
//console output

FINALLY
Exception in thread "main" ExceptionTest$MyException
    at ExceptionTest.sf1(ExceptionTest.java:11)
    at ExceptionTest.main(ExceptionTest.java:25)

4. Others:

1) The function can declare an exception but does not throw it.

2) if an exception is caught inside the function and no new exception is thrown in catch and finally, function declaration is not required. Function declaration exceptions are required only when no exceptions are caught in the function.

3) when a subclass refactor a function whose parent class contains an exception declaration, it cannot add a new exception Declaration (not an extended exception declared in the parent class method), but can reduce it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.