[Java] Java Exception Handling

Source: Internet
Author: User

Java treats exceptions as objects and defines a base class java. Lang. throwable as the superclasses of all exceptions.

The throwable class is a super class for all exceptions and errors. It has two subclasses: Error and exception, indicating errors and exceptions respectively. Exception exceptions are classified into runtimeexception and non-runtime exception. They are also called unchecked exception and checked exception ).

Error isProgramUnhandled errors, such as outofmemoryerror and threaddeath. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to terminate the thread.
Exception is an exception that can be handled by the program itself. There are two types of exceptions: runtime exceptions and non-runtime exceptions. The program should try to handle these exceptions.
Runtime exceptions are runtimeexception classes and their subclasses, such as nullpointerexception and indexoutofboundsexception. These exceptions are not checked and can be captured or not processed in the program, if it is not processed, you do not need to use throws in the method definition. These exceptions are generally caused by program logic errors. We should avoid such exceptions during programming.
Non-runtime exceptions are exceptions other than runtimeexception, And all types belong to the exception class and its subclass. From the perspective of program syntax, it is an exception that must be processed. If it is not processed, the program cannot be compiled. Such as ioexception, sqlexception, and user-defined exceptions.

Precautions for try, catch, and finally statement Blocks
1> try, catch, and finally statement blocks cannot be used independently. The three can be used as try... catch... finally, try... catch, try... three finally structures: one or more catch statements and one at most.
2> try, catch, and finallyCodeThe scope of the variables in the block is inside the code block, which is independent of each other and cannot be accessed from each other. If you want to access all three blocks, you need to define the variables outside these blocks.
3> when multiple catch blocks exist, only one of the exception classes is matched and the catch block code is executed. Other catch blocks are not executed, and the matching order of catch statements is from top to bottom.
Throw and throws keywords
The throw keyword is used inside the method body to throw a throwable exception. If a check exception is thrown, you should also use throws to declare the type of exception that the method may throw in the method header. The caller of this method must also check for exceptions thrown during processing.If all methods throw the obtained exceptions layer by layer, the JVM will process the exceptions and print the exception messages and stack information.If an error or runtimeexception is thrown, the caller of this method can choose to handle the exception.
The throws keyword is used in the method declaration part outside the method body to declare a method. Some exceptions may be thrown. Only when a check exception is thrown, the caller of the method must handle the exception or throw the exception again.When the caller of the method is unable to handle this exception, it should continue to throw, instead of being unable to handle it. Generally, the Catch Block prints the stack information to barely handle it.

If the check exception cannot be effectively handled, it can be converted to runtimeexception and thrown again. In this way, the upper-Layer Code has a choice-either processing or not. ----------- Recommended methods in Java programming ideas

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.