In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are java exceptions and how to deal with them", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the java exceptions and how to deal with them?"
What is an anomaly?
The exception of the program means that some unexpected events occur while the program is running, and these events prevent the program from executing normally as expected, which is the exception.
For example: the program needs to open a file and do some processing, but the file does not exist. The java code runs to the step of opening the file, because without this file, there is no way to open the file, and if there is no way to open the file, there is no way to process the file. This is an exception (a file opening exception).
The cause of the exception
Because some unexpected environments may occur when the program is running, the exceptions are different, for example, if the file does not exist, there will be an exception in the operation of opening the file. It is also possible that the java virtual machine memory overflow can also cause exceptions (usually this problem occurs when the code is not optimized). There are also exceptions caused by programmers who do not comply with the specification during development (for example, divisor cannot be zero, zero exception will be reported when the divisor is zero, and array operation out of bounds exception and null pointer exception, these are common exceptions for beginners).
Classification of anomalies
According to the example given above, java exceptions can be roughly divided into three categories, namely:
Error Error
This exception is a system-level exception, such as a Java virtual machine memory overflow. This exception does not occur at compile time or at run time, but can occur when used improperly. For example, when the maximum available memory of the java virtual machine is 16m, constantly append characters to the StringBuffer, so that the memory will soon be used up, and an OutOfMemoryError exception will occur.
Runtime exception RuntimeException
This exception usually occurs at run time, so it is called a run-time exception. This exception does not occur at compile time, and once the code runs, an exception occurs. For example, the division by zero operation, such code can be compiled through, but at run time will report an error. Along with the zero division exception are run-time exceptions: null pointer exception, out of bounds of the following table, and so on.
Traceable abnormal CheckedException
This exception is an exception that must be handled. This kind of exception usually reports an error at compile time, for example, there is no exception in the file mentioned earlier. This exception must be handled, otherwise the compiler will not pass the compilation, and the handling is the use of the try-catch statement that I will introduce next.
Exception handling
In java, there is a set of keywords used to catch exceptions, which is try-catch-finally. The code is as follows:
Try {/ / code that may throw an exception} catch (exception type 1 exception variable name 1) {/ / when the exception is thrown, the corresponding catch statement / / catch statement is executed according to the type and variable name of the exception to catch the exception. } catch (exception type 2 exception variable name 2) {/ / program code} finally {/ / regardless of the final outcome, statements that must be executed, such as cleaning up types, are generally done. / / finally is not necessary}
In addition, there is a set of keywords used to throw an exception, which is the throws/throw keyword:
If a method does not catch a traceable exception, the method must be declared using the throws keyword. The throws keyword is placed at the end of the method signature.
You can also use the throw keyword to throw an exception, whether it is newly instantiated or just caught.
The declaration of the following method throws a RemoteException exception:
Import java.io.*;public class className {public void deposit (double amount) throws RemoteException {/ / Method implementation throw new RemoteException ();} / / Remainder of class definition}
A method can declare that multiple exceptions are thrown, separated by commas.
For example, the following method declaration throws RemoteException and InsufficientFundsException:
Import java.io.*;public class className {public void withdraw (double amount) throws RemoteException, InsufficientFundsException {/ / Method implementation} / / Remainder of class definition} what exception needs to be caught
Usually errors, run-time exceptions, and traceable exceptions can be caught, but in general only detectable exceptions can be caught (because you can't even compile without catching them).
The above is about the content of this article on "what are the java exceptions and how to deal with them?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.