Introduction to Exceptions in Java

Source: Internet
Author: User
Tags finally block

I. Overview of exceptions

Exception handling has become one of the criteria for measuring the maturity of a language, and current mainstream programming languages such as C + +, C #, Ruby, Python, and so on, provide exception handling mechanisms. After adding the exception handling mechanism, the program has better fault tolerance and more robust.  Second, the shortcomings of traditional error handling-can not be exhaustive all abnormal situation: because of human knowledge, abnormal situation is always more than can be considered, there is always "slip through the gap" anomalies, so the program is always not strong enough. – Error handling code and business implementation code clutter: This kind of error handling and business implementation of mixed code seriously affects the readability of the program, which can increase the difficulty of program maintenance.  Third, Java exception System Iv. exception information All exception objects contain the following common methods: –getmessage (): Returns the detailed description string for the exception.  –printstacktrace (): Outputs the trace stack information for this exception to the standard error output.  –printstacktrace (PrintStream s): Outputs the trace stack information for this exception to the specified output stream. –getstacktrace (): Returns the trace stack information for this exception. V. finally
try{the code that needs to be detected;}catch (Exception class  variable) {exception handling code; code that}finally{is bound to execute;}

The code in finally is bound to execute, so you can use finally to reclaim resources.

The program opens some physical resources (such as database connections, network connections, and disk files) in a try block that must be explicitly reclaimed. To ensure that the physical resources opened in the try block can be reclaimed, the exception handling mechanism provides a finally block. The finally block is always executed regardless of whether the code in the try block has an exception, and no matter which catch block is executed.

Note : There is only one case where the finally code block will not be executed. It was before the System.exit (0) was executed.

Six, multi-anomaly capture

Before Java 7, each catch block could catch only one exception. But starting with Java 7, a catch block can catch multiple exceptions.

catch (Exception 1 | exception 2 | exception 3 ex) {}

Multiple exceptions are separated by a vertical line.

When multiple exception snaps, there is an implicit final modification before the exception variable.  The Printstacktrace method of exception tracking stack exception object is used to print the tracking stack information of the exception, according to the output of the Printstacktrace method, we can find the source of the anomaly and track the process of triggering the anomaly. When an object-oriented application runs, a series of method calls are often made, resulting in a "method call stack", and the exception is propagated in the opposite direction: as long as the exception is not fully captured (including the exception is not captured, or the exception is processed and the new exception is re-thrown), the exception is gradually propagated outward from the method in which First passed to the caller of the method, and the method caller creates the caller again ... Until it is finally uploaded to the main method, if the main method still does not handle the exception, the JVM aborts the program and prints the exception's trace stack information. Viii. exception handling rules do not overuse exceptions do not use an overly large try block to avoid catching an exception by using the Catch all statement

Introduction to Exceptions in Java

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.