Java exception Classification

Source: Internet
Author: User
Questions asked by the architect
1. Exception Mechanism
The exception mechanism refers to how the program handles errors. Specifically, the exception Mechanism provides a secure channel for program exit. When an error occurs, the execution process of the program changes, and the control of the program is transferred to the exception processor.

The traditional way to handle exceptions is that a function returns a special result to indicate exceptions (this special result is usually agreed ), the program that calls this function is responsible for checking and analyzing the results returned by the function. This method has the following drawbacks: for example, if the function returns-1, it indicates an exception, but if the function does want to return the correct value of-1, it will be confused; the readability is reduced, the program code is mixed with the code that handles exceptions. The program that calls the function analyzes errors, which requires the client programmer to have a deep understanding of the library functions. Exception Handling process: ① if an error occurs, the method ends immediately without returning a value. At the same time, an exception object is thrown. ② The program that calls this method will not continue to execute, but will search for an exception processor that can handle the exception and execute the code.

2. Exception Classification
Exception category:
① Inheritance structure of exceptions: The base classes include throwable, error, and exception, which inherit from throwable, runtimeexception, and ioexception. The specific runtimeexception inherits from runtimeexception.
② Error, runtimeexception and its subclass become unchecked, and other exceptions become checked ).

Characteristics of each type of exception
Error System:
The error class system describes internal errors in the Java running system and resource depletion. Applications should not throw such objects (usually thrown by virtual machines ). In addition to trying to secure the exit of the program, this error is not feasible in other aspects. Therefore, when designing programs, we should pay more attention to the exception system.

The exception system includes the runtimeexception system and other non-runtimeexception systems:
① Runtimeexception: The runtimeexception system includes incorrect type conversion, out-of-bounds array access, and attempts to access null pointers. The principle for processing runtimeexception is: If runtimeexception occurs, it must be a programmer error. For example, you can check the array subscript and array boundary to avoid array out-of-bounds access exceptions.
② Other non-runtimeexception (ioexception, etc.): This type of exception is generally an external error, such as an attempt to read data from the end of the file. This is not a program error, an external error occurs in the application environment.

Different from C ++ exception classification:
① The runtimeexception class name in Java is not appropriate, because any exception occurs at runtime. (The errors that occur during compilation are not exceptions. In other words, exceptions are used to solve the errors that occur during the program running ).
② Logic_error in C ++ is equivalent to runtimeexception in Java, while runtime_error is equivalent to non-runtimeexception type exceptions in Java. 3. Usage of exceptions
The declarative method throws an exception.
① Syntax: throws (omitted)
② Why should I declare a method to throw an exception?
Whether a method throws an exception is as important as the type of the method return value. If the method throws an exception and does not declare that the method throws an exception, the client programmer can call this method without writing code to handle the exception. In this case, once an exception occurs, no suitable exception controller can be used to solve the exception.
③ Why must I have checked the thrown exception?
Runtimeexception and error can be generated in any code. They do not need to be thrown by the programmer. Once an error occurs, the corresponding exception is automatically thrown. The check exception is thrown by the programmer. There are two situations: the client programmer calls the library function that throws an exception (the Library Function exception is thrown by the library programmer ); the client programmer throws an exception using the throw statement. When an error occurs, programmers are generally powerless. When a runtimeexception occurs, the program must have a logic error and must be modified (equivalent to a debugging method ); only the checked exceptions are the concern of the programmer. The program should and should only throw or handle the checked exceptions.
Note: The subclass method that overwrites a method of the parent class cannot throw more exceptions than the parent class method. Therefore, when designing a method of the parent class, an exception is declared and thrown, however, the Code of the actual implementation method does not throw an exception, so that the subclass method can throw an exception when overwriting the parent class method. How to throw an exception
① Syntax: Throw (omitted)
② What exceptions are thrown? For an exception object, the object type is abnormal when the information is really useful, and the exception object itself is meaningless. For example, if the type of an exception object is classcastexception, this class name is the only useful information. Therefore, when selecting an exception to be thrown, the most important thing is to select the exception class name to clearly indicate the exception.
③ Exception objects usually have two constructor types: one is a non-parameter constructor and the other is a constructor with a string, this string will be used as an additional description for this exception object except for the type name.
④ Create your own exceptions: When Java built-in exceptions cannot clearly describe the exceptions, you need to create your own exceptions. It should be noted that the only useful information is the type name, so do not spend effort on the design of the exception class. Capture exceptions
If an exception is not handled, the program is aborted and the exception information is output for a non-graphic program. For a graphic program, the exception information is also output, but the program does not stop, but returns an error page. Syntax: Try, catch, and finally (omitted). The controller module must be followed by the try block. If an exception is thrown, the exception Control Mechanism searches for the first controller whose parameters match the exception type. Then it enters the catch clause and considers that the exception has been controlled. Once the catch clause ends, the search for the Controller will also stop.
Capture multiple exceptions (note the syntax and capture sequence) (omitted)
Finally usage and exception handling process (omitted)
What is exception handling? For Java, due to garbage collection, the memory does not need to be recycled for exception handling. However, there are still some resources that need to be collected by programmers, such as files, network connections and images.
Should I declare whether the method throws an exception or catch an exception in the method? Principle: capture and handle exceptions that know how to handle, and pass exceptions that do not know how to handle.
Throw an exception again
① Why should I throw an exception again? At this level, only a part of the content can be processed. Some processing needs to be completed in a higher-level environment, so an exception should be thrown again. In this way, the exception processor at each level can handle the exceptions that it can handle.
② Exception handling process: the Catch Block corresponding to the same try block will be ignored, and the thrown exception will enter a higher level. 4. Other exceptions
① Excessive use exception: first, it is very convenient to use exceptions, so programmers are generally no longer willing to write code to handle errors, rather than simply throwing an exception. This is wrong. For completely known errors, you should write the code to handle such errors to improve the robustness of the program. In addition, the exception mechanism is inefficient.
② Differentiate exceptions from common errors: for common errors that are exactly the same, you should write the code to handle these errors to improve the robustness of the program. An exception is required only for unknown and unexpected running errors.
③ Information contained in the exception object: Generally, the only useful information of the exception object is the type information. However, when a constructor with an exception string is used, this string can also be used as additional information. You can call the getmessage (), tostring (), or printstacktrace () Methods of an exception object to obtain additional information about the exception object, class name, and call stack information. In addition, the information contained in the latter type is the superset of the former type.

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.