Java Learning--exception handling and its application classes

Source: Internet
Author: User
Tags class definition finally block throwable

An exception is an unhealthy condition that is caused by the runtime in a code sequence, in other words, an exception is a run-time error. In computer languages that do not support exception handling, errors must be checked and handled manually----usually by using error codes, and so on. This is awkward and cumbersome. Exception handling in Java avoids these problems and manages run-time errors in an object-oriented manner during processing.

1, the basic concept of exception handling

A Java exception is an object that describes an exception (that is, an error) that occurs in a piece of code. When an exception occurs, an object is created to represent the exception, and an exception is thrown in the method that caused the error. Method can choose to handle the exception itself, or you can continue to pass the exception. Either way, the exception is caught and handled at some point. Exceptions can be generated by the Java runtime system or manually by code. Exceptions thrown by Java are related to underlying errors that violate the Java language Rules or Java Execution Environment constraints. Manually generated exceptions are typically used to report certain error conditions to callers of the method.

Java exception handling is managed by 5 keywords: try, catch, throw, throws, and finally. What they do: Encapsulate program statements that may occur in a try code block and monitor those statements. If an exception occurs in a try code block, the exception is thrown. The code can catch exceptions (using catch) and handle them in some rational way. System-generated exceptions are automatically thrown by the Java runtime System. In order to throw exceptions manually, you need to use the Throw keyword. Any exception thrown from a method must be specified by a throws statement. All of the code blocks that must be executed after the try block has ended need to be put into a finally block of code.

Exception handling code block general form:

try{
Block of code to moniter for errors
}
catch (ExceptionType1 e) {
Exception handler for ExceptionType1
}
catch (ExceptionType2 e) {
Exception handler for ExceptionType1
}
//.........
finally{
Block of code to being executed after try Bloock ends
}

Where ExceptionType2 is the type of exception that has occurred. In addition, starting with JDK7, the try statement adds a new form of support for automatic resource management, which is called a try with a resource.

2. Exception type

All exception types are subclasses of the built-in class Throwable. Therefore, the Throwable class is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses, who divide the exception into two different branches. A branch is the exception class, which can be used either for exceptions that the user program should catch or to create subclasses of the custom exception type. The exception class has an important subclass called the RuntimeException class. For programs that are written, this type of exception is automatically defined, including cases such as 0 and invalid array indexes.

Another branch is the error class, which defines exceptions that are not expected to be caught by the program in a normal environment. An exception of type error is used by the Java runtime system to indicate that there are some errors in the runtime environment itself. Stack overflows are an example of such errors, which are often created in response to catastrophic failures, and programs typically cannot handle such exceptions. Top-level exception levels

3, throws/throw key words:

If a method does not capture an inspection exception, the method must be declared using the throws keyword. The throws keyword is placed at the tail end of the method signature. You can also throw an exception with the Throw keyword, whether it's newly instantiated or just captured. The declaration of the following method throws a RemoteException exception:

      Import java. Io. *;

Public class classname {

< Span class= "Hl-identifier" > public void deposit (double amount) throws remoteexception { /span>

< Span class= "hl-brackets" > // Method implementation throw new Remoteexception

< Span class= "Hl-identifier" > < Span class= "Hl-code" >} / span>

< Span class= "Hl-identifier" >< Span class= "hl-comment" > //remainder of class definition

< Span class= "hl-comment" > }

A method can declare that multiple exceptions are thrown, and multiple exceptions are separated by commas. The following method declarations throw remoteexception and insufficientfundsexception:

Import java. Io. *;

Public class className {

Public void withdraw (double amount< Span class= "Hl-brackets" >) throws remoteexception Insufficientfundsexception < Span class= "Hl-identifier" >< Span class= "hl-brackets" > { /span>

< Span class= "Hl-identifier" >< Span class= "hl-brackets" > < Span class= "hl-comment" >// Method implementation

< Span class= "hl-brackets" > < Span class= "hl-reserved" >< Span class= "hl-comment" > }

< Span class= "hl-types" > < Span class= "hl-reserved" >< Span class= "hl-comment" > //remainder of class definition span>

< Span class= "Hl-identifier" > < Span class= "hl-comment" > }

4. The Finally keyword

The finally keyword is used to create a block of code that executes after a try code block. The code in the finally code block is always executed, regardless of whether an exception occurs. In the finally code block, you can run an end-of-nature statement such as cleanup type.

Java Learning--exception handling and its application classes

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.