Javase Basic Review strategy "four"

Source: Internet
Author: User

This article focuses on the exception handling mechanism in Java, what is a Java exception? Java exceptions are a mechanism provided by Java for handling errors in a program. The so-called error refers to some unusual events that occur during the running of the program (for example: 0 overflow, the following table is out of bounds, the file being read does not exist ...).

During the execution of a Java program, you can generate an exception class object that encapsulates the information of the exception event and will be presented to the Java Runtime system, a process known as throw (throw) exceptions, if an exception event occurs. When the Java runtime system receives an exception object, it looks for code that can handle the exception and hands the current object to its processing, a process known as catch (Catch) exception.

The Java SDK defines a number of exceptions, broadly divided into: error--errors, generated and thrown by a Java virtual machine, including: Dynamic link failure, virtual machine error, and no action by the program. exception--the parent class of all exception classes, whose subclasses correspond to this variety of possible exception events, typically require explicit declaration and capture by the user. runtimeexception--run-time error, a special kind of exception, such as by 0, array subscript out of bounds, and so on, this kind of anomaly generated more frequent, processing trouble, if the explicit declaration and capture will have a significant impact on the readability and operational efficiency of the program. Therefore, the system automatically detects them and gives them to the default exception handlers (which the user does not have to handle).

1, the exception of the capture and processing:

Java captures and handles exceptions using the try and catch keywords. The try snippet contains code that can produce exceptions, a try snippet followed by one or more catch code snippets, each catch snippet declares a particular type of exception that it can handle, and provides a handling method, and when an exception occurs, the program aborts the current process. The corresponding catch code snippet is executed according to the type of exception caught, and the code of the finally segment is processed regardless of whether an exception occurred.

   

2, the exception of the capture and processing:

 Public classTest {/**     * @paramexception capture and handling*/    Private StaticFileInputStream input;  Public Static voidMain (string[] args)throwsFileNotFoundException {Try{//Catching exceptionsinput =NewFileInputStream ("Test.java"); intA =Input.read ();  while(A!=-1) {System.out.println (Char) a); A=Input.read (); }            } Catch(IOException e) {e.printstacktrace (); System.out.println ("System Exception"); }finally{System.out.println ("Close Program"); }    }}

For the exception of the processing and capture for everyone to summarize these, if you have any ideas, welcome message to add, together to exchange common progress.

Javase Basic Review strategy "four"

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.