Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common Java exceptions?

Shulou Source: shulou.com Published: 2022-06-02 11:02:47 09月25日 Update

This article shows you what common Java exceptions are, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Exceptions are common problems in Java programs. I think every Java programmer hates exceptions. An exception is a BUG, and it takes a lot of time to locate the exception problem.

Today, let's take a look at the top 10 exceptions that you often encounter in Java, in no particular order.

1 、 NullPointerException

Null pointer exception, which is thrown when manipulating a method or property of a null object.

2 、 OutOfMemoryError

Memory exception, which is beyond the control of the program, means that the memory of the object to be allocated exceeds the current heap memory, and it is necessary to adjust the heap memory size (- Xmx) and optimize the program.

3 、 IOException

IO, that is, input, output, is an exception that often occurs when we read and write disk files and network content. This exception is checked and needs to be caught manually.

IOException will be thrown if the file is read or written:

Public int read () throws IOException public void write (int b) throws IOException

4 、 FileNotFoundException

The file cannot find an exception, which will be thrown if the file does not exist.

If the input and output file stream is defined, an error will be reported if the file does not exist:

Public FileInputStream (File file) throws FileNotFoundException public FileOutputStream (File file) throws FileNotFoundException

FileNotFoundException is actually a subclass of IOException. It is also a checked exception and needs to be caught manually.

5 、 ClassNotFoundException

Class can not find exceptions, often encountered in Java development, is not very desperate? This is thrown when the class is loaded, that is, the specified class cannot be loaded under the classpath.

Look at an example:

Public static Class getExistingClass (ClassLoader classLoader, String className) {try {return (Class) Class.forName (className, true, classLoader);} catch (ClassNotFoundException e) {return null;}}

It is a checked exception and needs to be caught manually.

6 、 ClassCastException

Class converts an exception, which is thrown if an instance that is not the class is converted to this class.

If you cast a number into a string, you will report this exception:

Object x = new Integer (0); System.out.println ((String) x)

This is a run-time exception and does not need to be caught manually.

7 、 NoSuchMethodException

There is no exception to this method, which usually occurs when the reflection calls the method, such as:

Public Method getMethod (String name, Class... ParameterTypes) throws NoSuchMethodException, SecurityException {checkMemberAccess (Member.PUBLIC, Reflection.getCallerClass (), true); Method method = getMethod0 (name, parameterTypes, true); if (method = = null) {throw new NoSuchMethodException (getName () + "." + name + argumentTypesToString (parameterTypes));} return method;}

It is a checked exception and needs to be caught manually.

8 、 IndexOutOfBoundsException

Index out of bounds exception, an exception that is often encountered when manipulating a string or array.

As shown in the figure, it is a run-time exception and does not need to be caught manually.

9 、 ArithmeticException

An exception that occurs in the arithmetic operation of a number, such as a number divided by 0.

Double n = 3 / 0

Although this exception is a runtime exception, you can manually catch and throw custom exceptions, such as:

Public static Timestamp from (Instant instant) {try {Timestamp stamp = new Timestamp (instant.getEpochSecond () * MILLIS_PER_SECOND); stamp.nanos = instant.getNano (); return st} catch (ArithmeticException ex) {throw new IllegalArgumentException (ex);}}

10 、 SQLException

SQL exception, the exception that occurs when the database is manipulated.

Get the connection as follows:

Public Connection getConnection () throws SQLException {if (getUser () = = null) {return DriverManager.getConnection (url);} else {return DriverManager.getConnection (url, getUser (), getPassword ());}}

Or when it comes to getting the next record:

Boolean next () throws SQLException

It is a checked exception and needs to be caught manually.

What are the common Java exceptions mentioned above? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Manual file time check memory programs content numbers methods will report run common characters strings objects that is skills knowledge arithmetic problems Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft MariaDB Docker Huawei