In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the differences between Throw and throws in JAVA". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the differences between Throw and throws in JAVA?"
Preface
If a method cannot complete the task in the normal way, it can exit the method through another path, in which case an object encapsulating the error message will be thrown. In this case, the method immediately exits and does not return any value, other code that calls this method cannot continue to execute, and the exception handling mechanism gives the code execution to the exception handler.
Throwable is the superclass of all errors or exceptions in the JAVA language, which is divided into error or Exception at the next level.
Error:
The Error class refers to the internal errors and resource exhaustion errors of the system, and the application will not throw this kind of object. If such an error occurs, the application will try its best to terminate the program safely.
Exception has two branches, one is abnormal RuntimeException at run time, and the other is to check for abnormal CheckedException.
RuntimeException such as: NullPointerException, ClassCastException; one is to check for abnormal CheckedException, such as IOException and SQLException caused by the error of I _ pico. RuntimeException is a superclass of exceptions that may be thrown during the normal operation of the Java virtual machine. If there is a RuntimeException, it must be your own fault.
Check exception CheckedException: generally, it is an external error. This kind of exception occurs during the compilation phase. The Java compiler will force the program to catch this kind of exception, that is, it will require you to try catch the program that may have an exception. This kind of exception includes several aspects:
1. Attempt to read data at the end of the file
two。 An attempt was made to open a malformed URL
3. An attempt is made to find a class object based on a given string, but the class represented by this string does not exist, and so on.
Exceptions are handled as follows:
If the problem is not dealt with, continue to throw it to the caller. There are three ways to throw an exception, one is throw, the other is throws, and the other is that the system automatically throws an exception.
Public static void main (String [] args) {String s = "abc"; if (s.equals ("abc")) {throw new NumberFormatException ();} else {System.out.println (s);}} int p (int a minint b) throws Exception {return a gamb;}
Targeted handling of thr catch catch exceptions.
The difference between Throw and throws:
Different locations
1. Throws is used in functions, followed by exception classes, which can be followed by multiple, while throw is used in functions, followed by exception objects. The functions are different:
2. Throws is used to declare exceptions, so that the caller only knows the possible problems with the feature and can give a pre-handler. When throw throws a specific problem object and executes it to throw, the function ends, jumps to the caller, and throws the specific problem object to the caller. In other words, when the throw statement exists independently, do not define other statements below, because it cannot be executed.
3. Throws indicates a possibility of exceptions, which may not necessarily occur; throw throws an exception, while executing throw must throw some exception object.
4. Both are negative ways of handling exceptions, just throwing or possibly throwing exceptions, but the exception is not handled by the function, and the real exception is handled by the upper layer of the function.
At this point, I believe you have a deeper understanding of "what is the difference between Throw and throws in JAVA". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.