Back to Java Memoirs (ix): Use of java09 exceptions

Source: Internet
Author: User
Tags throw exception




What is an exception: the error generated by the Java program Runtime, which encapsulates all the resulting exceptions into an object called Throwable, which is the parent of all exceptions


Throwable contains 2 sub-classes of Error and Exception

Error: The rep is a 1 program error, especially a low-level, non-recoverable, serious error

Performance: 1. Error occurs when the program exits because the physical environment required to run the program is lost

2. We are unable to process error

Exception: Due to specific factors, the program cannot continue, but does not affect the JVM's normal operation

No exception checked (Runntime Exception)

No necessary checks are made by the programmer, the program can be compiled successfully, the runtime error occurs (memory overflow ...)

Checked for exceptions (non-runntime Exception)

Exceptions are unavoidable and must be handled abnormally, otherwise it cannot be compiled (IO operation, net,thread,jdbc ...)

Exception handling mechanism:

1. When the program is written: A statement may have an exception

The 2.Java mechanism throws a throw to an exception object (xxxexception)

3. The statement after the program does not execute, automatically returns to the previous level method, and the previous method accepts the exception object for processing

4. Treatment of the upper-level approach

--the method has the ability to process (process in this method)

--the method does not have the ability to process (continue to throw), if the current method is main, he throws the JVM, which causes the JVM to terminate the program's run

How the exception is handled

Throws and try-catch--[finally] blocks complete

Problems when writing exceptions

1. Try---correspond to several catch or a finally: the main observation of the try-catch-finally appearance of the hierarchy problem

Abnormal use of 2.Throws and THORW

Throws: Indicates that a method may produce an exception when executed, only for the method's declared format: [method name Throws Xxxexception]

Throw: can only be written in the method, which indicates that an exception format is generated actively [throw exception object]

Custom exceptions

Write a class that inherits Exception or Inherits Runntimeexception



Back to Java Memoirs (ix): Use of java09 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.