Java Exception Handling

Source: Internet
Author: User
Tags finally block

1. Exception classification

  

All exception classes in Java are derived from the Throwable class, and the Throwable class has subclass error and exception class. Where error represents an internal error for the Java Runtime System, exception indicates that the program is running abnormally. The subclass of exception is divided into two main classes: RuntimeException class and other exception class, RuntimeException represents the logic error of the program, such as: nullpointerexception; other exceptions represent errors caused by external exception conditions. such as: IOException.

Java divides exceptions into checked and unchecked exceptions, non-checked exceptions include error and runtimeexception, and checked exceptions include other exception. An unchecked exception indicates that the error is either not controlled by the programmer (error), or that the programmer should avoid it altogether (runtimeexception). Just checking for exceptions means errors thrown by external conditions, and the exception handling mechanism in Java is primarily to handle checked exceptions.

2. Overrides and exceptions

Subclasses want to overwrite a method in the parent class, and if the method does not throw an exception in the parent class, the new method in the subclass cannot throw an exception, and if the method throws an exception in the parent class, the subclass can not throw an exception, and if an exception is thrown, the method in the subclass is required to throw an exception that does not exceed the exception

3.finally clause

The finally clause indicates that the code in the finally block is executed regardless of whether an exception occurred in the try block. Finally, it is commonly used to release resources that need to be manually freed in the program, such as closing files, shutting down database connections, and so on. Cases:

Try {  try  {     
return 1; } finally { input.close ();
return 2;} } Catch (IOException e) { e.printstacktrace ();}

It is important to note that in the example above, the try statement inside has only a finally clause, which is actually a valid and recommended way to indicate that the try statement is intended to free resources and, in many cases, the code in the finally clause may throw an exception.

Assuming that the Try statement contains a return statement, and the finally clause also contains a return statement, the return statement in finally overwrites the returned value in the Try statement.

  

Java Exception Handling

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.