In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you what is the learning experience about Java anomalies. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
one。 Abnormal mechanism
The exception mechanism refers to how the program handles when an error occurs. Specifically, the exception mechanism provides a safe channel for the program to exit. When an error occurs, the process of the program execution changes and the control of the program is transferred to the exception handler.
1. Ways to handle exceptions
The traditional way to handle exceptions is that the function returns a special result to indicate the exception (usually this special result is commonly known as), and the program that calls the function is responsible for checking and analyzing the results returned by the function. This has the following disadvantages: for example, the function returns-1 represents an exception, but it will be confused if the function does return the correct value of-1; it is less readable and mixes the program code with the code that handles the exception; the error is analyzed by the program that calls the function, which requires the customer programmer to have a deep understanding of the library function.
two。 The flow of exception handling
When an error is encountered, the method ends immediately and does not return a value; at the same time, an exception object is thrown
The program that calls the method does not continue to execute, but instead searches for an exception handler that can handle the exception and executes the code in it
two。 Classification of anomalies
Exception inheritance structure: the base classes are Throwable,Error and Exception inheriting Throwable,RuntimeException and IOException inheriting Exception, and specific RuntimeException inheriting RuntimeException.
Error and RuntimeException and their subclasses become unchecked exceptions (unchecked), and other exceptions become checked exceptions (checked).
1. The characteristics of each type of exception:
(1) Error system
The Error class architecture describes the internal errors and resource exhaustion in the Java operating system. Applications should not throw objects of this type (typically thrown by virtual machines). If such an error occurs, there is nothing you can do except to make the program exit safely. Therefore, when programming, we should pay more attention to the Exception system.
(2) Exception system
Exception system includes RuntimeException system and other non-RuntimeException system.
(3) RuntimeException
The RuntimeException architecture includes incorrect type conversions, array out-of-bounds access, attempts to access null pointers, and so on. The principle for dealing with RuntimeException is: if RuntimeException occurs, it must be the programmer's fault. For example, you can avoid array out-of-bounds access exceptions by checking array subscripts and array boundaries.
(4) other (IOException, etc.)
Such exceptions are generally external errors, such as trying to read data from the end of the file, which is not the fault of the program itself, but an external error that occurs in the application environment.
two。 Different from the abnormal classification of C++
In fact, the name RuntimeException in Java is not appropriate, because any exception occurs at run time. Errors that occur at compile time are not exceptions; in other words, exceptions are meant to resolve errors that occur when the program is running.
Logic_error in C++ is equivalent to RuntimeException in Java, while runtime_error is equivalent to non-RuntimeException type exceptions in Java.
three。 The use of exceptions
1. Why declare that the method throws an exception?
Whether the method throws an exception is as important as the type of value that the method returns. Assuming that a method throws an exception without declaring that the method will throw an exception, the client programmer can call the method without writing code to handle the exception. Then, once an exception occurs, there is no appropriate exception controller to resolve the exception.
two。 Why does the exception thrown have to be checked?
RuntimeException and Error can be generated in any code, they do not need to be thrown by the programmer, once an error occurs, then the corresponding exception will be automatically thrown. The check exception is thrown by the programmer, which is divided into two cases: the client programmer calls the library function that throws the exception (the exception of the library function is thrown by the library programmer); the customer programmer uses the throw statement to throw the exception. Programmers are generally powerless when they encounter Error; when they encounter RuntimeException, they must have logic errors in the program and modify the program (equivalent to a method of debugging); only checked exceptions are of concern to programmers, and programs should and should only throw or handle checked exceptions.
Note: a subclass method that overrides a method of the parent class cannot throw more exceptions than the method of the parent class, so sometimes the method of the parent class will be declared to throw an exception, but the actual code that implements the method will not throw an exception. The purpose of this is to facilitate the subclass method to override the parent class method can throw an exception.
3. What exception is thrown?
For an exception object, the really useful information is the object type of the exception, and the exception object itself is meaningless. For example, if the type of an exception object is ClassCastException, then the class name is * useful information. Therefore, when choosing what exception to throw, the most important thing is to select the class name of the exception that clearly describes the exception.
There are usually two types of constructors for exception objects: one is a constructor with no arguments, and the other is a constructor with a string that will be used as an additional description of the exception object in addition to the type name.
Create your own exceptions: you need to create your own exceptions when none of the built-in exceptions in Java can clearly describe the exception. It is important to note that what is useful is the type name information, so don't spend energy on the design of exception classes.
4. Catch exception
If an exception is not handled, for a non-graphical program, the program is aborted and the exception information is output; for a graphical interface program, the exception information is also output, but the program is not aborted. Instead, it returns to the user interface processing loop.
5. What does exception handling do?
For Java, because of garbage collection, exception handling does not need to reclaim memory. But there are still some resources that programmers need to collect, such as files, network connections, and pictures.
6. Should you declare that the method throws an exception or catches the exception in the method?
Principle: catch and handle exceptions that know how to handle, and pass exceptions that don't know how to handle
7. If an exception is thrown again, why throw an exception again?
At this level, only part of the content can be processed, and some processing needs to be done in a higher-level environment, so an exception should be thrown again. This enables the exception handler at each level to handle the exceptions it can handle.
8. Exception handling flow
Catch blocks corresponding to the same try block are ignored and the exception thrown goes to a higher level.
four。 Other questions about exceptions
1. Overuse exception
First of all, exceptions are convenient, so programmers are generally no longer willing to write code that handles errors, but simply throw an exception. This is wrong, for fully known errors, we should write code to deal with such errors to increase the robustness of the program. In addition, the efficiency of the exception mechanism is very poor.
two。 Distinguish exceptions from ordinary errors
For common and completely consistent errors, code should be written to deal with such errors to increase the robustness of the program. Exceptions are required only for run-time errors that cannot be determined and predicted externally.
3. Information contained in exception objects
In general, the useful information about exception objects is type information. However, when using an exception constructor with a string, this string can also be used as additional information. Call the getMessage (), toString (), or printStackTrace () methods of the exception object to get additional information about the exception object, the class name, and the call stack, respectively. And the information contained in the latter is a superset of the former.
PS: the above is the author's java abnormal learning personal experience, slightly sorted out, perhaps a little confused, there is no good logical order, but still hope that everyone can get their own experience more or less from it.
The above is what the editor shares with you about the abnormal learning experience of Java. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.