In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "Java exception handling and common exceptions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Many events do not always develop smoothly according to people's own design wishes, and abnormal situations of one kind or another often occur. For example, you plan to go for an outing at the weekend, → from home to →, swim, →, barbecue, → and go home. But the weather is unexpected, when you are ready for a barbecue, it suddenly rains heavily, so you can only stop the outing and go home ahead of time. "heavy rain" is an abnormal situation, your plan should take into account this situation, and there should be a plan to deal with this exception.
The writing of computer programs also need to consider dealing with these exceptions. Exception is an exception that occurs when a program is running, and it has become one of the criteria to measure whether a language is mature or not. At present, the mainstream programming language java also provides exception handling mechanism.
Exception introduction
An exception in Java, also known as an exception, is an event that occurs during the execution of a program that interrupts the normal flow of instructions of the executing program. In order to deal with running errors in the program timely and effectively, exception classes must be used, which can make the program fault-tolerant and more robust.
There are three main reasons for an exception in Java:
An exception occurred in the Java internal error, which was generated by the Java virtual machine.
Exceptions caused by errors in written program code, such as null pointer exceptions, array out-of-bounds exceptions, etc.
An exception generated manually by a throw statement is generally used to inform the caller of the method of some necessary information.
Java handles exceptions through an object-oriented approach. During the operation of a method, if an exception occurs, the method produces an object that represents the exception and gives it to the runtime system, which looks for the appropriate code to handle the exception.
The process of generating an exception object and submitting it to the runtime system is called a throw exception. The runtime system looks in the method's call stack until it finds an object that can handle this type of exception, a process called catch.
Example 1
To better understand what an exception is, let's take a look at a very simple Java program. The following sample code implements an implementation that allows the user to enter integers within 1 to 3, and other cases prompt for input errors.
Package io.renren.config;import java.util.Scanner;/** * Created by LiYangYong * / public class TestException {public static void main (String [] args) {System.out.println ("Please enter your choice: (integer between 1 and 3)"); Scanner input = new Scanner (System.in); int num = input.nextInt () Switch (num) {case 1: System.out.println ("one"); break; case 2: System.out.println ("two"); break; case 3: System.out.println ("three"); break Default: System.out.println ("error"); break;}}
Normally, the user will follow the prompts of the system to enter a number between 1 and 3. However, if the user does not enter as required, such as a letter, the program will run with an exception, as shown below.
Please enter your choice: (integer between 1 and 3)
223sdf
Exception in thread "main" java.util.InputMismatchException
At java.util.Scanner.throwFor (Scanner.java:864)
At java.util.Scanner.next (Scanner.java:1485)
At java.util.Scanner.nextInt (Scanner.java:2117)
At java.util.Scanner.nextInt (Scanner.java:2076)
At io.renren.config.TestException.main (TestException.java:11)
At sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)
At sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
At java.lang.reflect.Method.invoke (Method.java:497)
At com.intellij.rt.execution.application.AppMain.main (AppMain.java:147)
Process finished with exit code 1
Exception type
In order to deal with the running errors in the program timely and effectively, Java specially introduces exception classes. All exception types in Java are subclasses of the built-in class java.lang.Throwable class, meaning that Throwable is at the top of the exception class hierarchy. There are two exception branches, Exception and Error, under the Throwable class, as shown in figure 1.
As you can see from figure 2, the Throwable class is the superclass of all exceptions and errors, with two subclasses, Error and Exception, representing errors and exceptions, respectively. The exception class Exception is divided into run-time exception and non-run-time exception. There is a great difference between these two kinds of exception, also known as unchecked exception (Unchecked Exception) and check exception (Checked Exception).
The Exception class is used for exceptions that may occur in user programs, and it is also a class used to create custom exception type classes.
Error defines exceptions that you don't want to be caught by a program in a normal environment. Generally refers to JVM errors, such as stack overflows.
This section does not discuss exception handling for Error types, as they are usually catastrophic and fatal errors that are beyond the program's control. Next, we will discuss exception handling of the Exception type.
Run-time exceptions are RuntimeException class and its subclass exceptions, such as NullPointerException, IndexOutOfBoundsException and so on. These exceptions are not checked, and can be captured or not handled in the program. These exceptions are generally caused by program logic errors, and the program should avoid such exceptions as much as possible from a logical point of view.
Non-runtime exceptions refer to exceptions other than RuntimeException, which belong to the Exception class and its subclasses in type. From the perspective of program syntax, it is an exception that must be handled, and if it is not handled, the program cannot be compiled and passed. Such as IOException, ClassNotFoundException, etc., and user-defined Exception exceptions (generally, no custom check exceptions).
Tables 1 and 2 list the types and functions of runtime exceptions and non-runtime exceptions defined in java.lang, respectively.
Exception type indicates ArithmeticException arithmetic error exception, such as zero divider ArraylndexOutOfBoundException array index out of bounds ArrayStoreException assigns values to array elements of incompatible types ClassCastException type conversion exception IllegalArgumentException invokes methods with illegal arguments lIIegalStateException environment or the application is in an incorrect state lIIegalThreadStateException the requested operation is incompatible with the current thread state IndexOutOfBoundsException some type of index out of bounds NullPointerException attempts to access null object members Null pointer exception NegativeArraySizeException the array created within the range of negative numbers NumberFormatException digital conversion format exception For example, conversion of string to float number invalid TypeNotPresentException type does not find exception type means ClassNotFoundException did not find class IllegalAccessException access class denied InstantiationException attempt to create abstract class or interface InterruptedException thread interrupts the domain of NoSuchFieldException request by another thread there is no NoSuchMethodException request method there is no ReflectiveOperationException reflection-related exception superclass "Java exception handling and common exceptions" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.