Java-exceptions in (30th sets)

Source: Internet
Author: User

The following is a java4android video tutorial from marschen.

Main content of this set.
1. What is an exception.
2. Exception classification.
3. Try... catch... use of finally structure.



1. Exception: an event that interrupts the normal command flow.
It is generated during the running process of the program and has no relationship with the compilation.
The program syntax is correct. Running may also cause exceptions.


 
2. Exception Classification
Exception classes provided by JDK.

Throwable --- | --> exception -----> --> | runtimeexception
| --> |
| --> Erorr


3. Try... catch instance.
Finally, the finally statement is executed no matter the exception occurs.


// Uncheck exceptions can be compiled without the try, catch, or catch conditions.

class Test{public static void main(String agrs[]){System.out.println(1);//uncheck exceptiontry{System.out.println(2);int i = 1 / 0 ;System.out.println(3);}catch(Exception e){e.printStackTrace();System.out.println(4);}finally{    System.out.println("finally");}System.out.println(5);}}

// Check exceptions. If try... catch... is not added, compilation fails.

class TestCheck{public static void main(String args[]){//check exceptiontry{Thread.sleep(1000);}catch(Exception e){e.printStackTrace();System.out.println(4);    }    finally{    System.out.println("finally");}}}

Finally is executed regardless of whether an exception occurs or not. When opening a file, we must disable it regardless of whether an error occurs.

Therefore, operations such as file close are suitable for storing in finally.

Summary:
1. programmers can't do anything about error. They can only handle exception.
2. Handling exceptions is related to the robustness of the system.
3. Use try... catch... finally to handle possible exceptions.

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.