A simple understanding of the application of exception handling in Java programming _java

Source: Internet
Author: User
Tags exception handling finally block throwable

A Java exception is an object that describes the condition of an exception (that is, an error) that occurs in a code snippet. When an exception occurs, an object representing the exception is created and thrown (throw) in the method that caused the error. The method can choose to handle the exception itself or pass the exception. In both cases, the exception is caught (caught) and processed. The exception may have been generated by the Java runtime System, or by your manual code. Exceptions thrown by Java are related to basic errors that violate the language specification or exceed the limitations of the Java execution environment. The exception generated by manual encoding is basically used to report the error condition of the method Invoker.

Java exception handling is controlled by 5 keywords: try, catch, throw, throws, and finally. Here's how they work. The program declares that the exception monitoring you want is contained in a try block. If an exception occurs in a try block, it is thrown. Your code can catch this exception (with catch) and handle the exception in a reasonable way. The system-generated exception is automatically thrown by the Java runtime System. Throws an exception manually, throw with the keyword. Any exception that is thrown by a method must be defined by the throws clause. Any code that is absolutely executed before the method returns is placed in the finally block.

The following is the usual form of an exception-handling block:

try {/
  /block ' Code to monitor for errors
}
catch (ExceptionType1 exob) {
  //exception handler for EXCE PtionType1
}
catch (ExceptionType2 exob) {
  //exception handler for ExceptionType2
}
//...
finally {
  //block of code to is executed before try block ends
}

Here, Exceptiontype is the type of exception that occurred.

All exception types are subclasses of the built-in class throwable. Therefore, Throwable is at the top of the hierarchy of exception classes. Immediately following Throwable are two subclasses that divide the exception into two distinct branches. One branch is exception.

This class is used for exceptions that a user program might catch. It is also a class that you can use to create your own subclass of user exception types. There is an important subclass of RuntimeException in the exception branch. This type of exception is automatically defined for the program you are writing and includes errors such as the 0 exception and the illegal array index.

The other branch is made up of error as the top layer, and error defines an exception that is not expected to be caught by the program in the usual environment. An exception of the error type is used by the Java Run-time system to display errors related to the run-time system itself. A stack overflow is an example of this error. Exception handling for error types will not be discussed in this chapter because they are usually catastrophic fatal errors that are not controlled by your program.

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.