Common Exceptions in Java!

Source: Internet
Author: User
Tags throwable

Most recently, there are a variety of exception bugs that are encountered during the writing of a project. It takes a lot of time to make a bug every time, so today it's time to sum up some common anomalies for your reference. There is no place to say is not very right, need to modify the can DMS me or comment.

First of all, we should be traced to the source code for the exception, so that we understand the deeper point. All exceptions are inherited Java. lang. Throwable class;

And the Throwable class has two direct subclasses is the error and exception; Here we'll introduce the error and Exception

One: error:error literally is the wrong meaning, he refers to the JVM (Java Virtual Machine) level of error, we programmers can not handle.

Second: Exception:exception also inspected the abnormal and non-inspected abnormalities. So the question is again, what is the subject of the abnormal and non-inspected anomaly?

The anomaly referred to is caused by some external factors, the procedure must deal with these anomalies;

and non-tested exceptions refer to the anomalies caused by the programmer's logic.

The Java compiler does not force processing (meaning that the compile period can be handled without processing, but the runtime must be handled).

Let's take a look at the most common exception classes for both tested and non-inspected exceptions:

inspected abnormality : 1. FileNotFoundException: refers to the file can not find an exception, the problem arises first look at your file name Chen write correctly no

or it's yours. There is a problem with the path.

led him find no to the

2: IOException: refers to the IO stream exception, this kind of problem is usually in the file picture upload download, code some local logic problems

or careless to write wrong code caused, the report of the exception will clearly indicate what went wrong, carefully look for .

3:SQLException: refers to the SQL statement exception, such a problem do not look, carefully examine their own code of the SQL statement, to see if the database name is wrong

either the column name is wrong, or your placeholder is in the wrong order, and your list is consistent with the database table name (in fact, the best way to solve this problem

The idea is to first write the SQL statement in the database and then copy it, and make sure it's not wrong.

4.InterruptedException: An interrupt exception that is typically encountered in a Java thread when the thread invokes wait () and sleep ()

, join (), and so on, the Interruptedexception exception is thrown (for example: the child thread calls the Sleep method to wait 5 seconds, while the unit test main thread

without setting the sleep method, it will execute quickly, forcibly interrupt the execution of the child thread, it will report the error, the solution is the main thread also set the Sleep method length

longer than 5 seconds).

non-inspected exception:

1.nullpointerexception: null pointer exception, this is everyone again familiar with the exception, this kind of problem is that you set the value of the parameter is not

Pass in, Java will be clear hint of the error in which line, debug your own debugging will know.

2.classcastexception: The popular point is the type conversion exception (incompatible), such problems often appear in inheritance, such as: Subclasses

and the conversion process of the parent class (it can be said to be a downward transition, for example, a dog is an animal, but an animal is not necessarily a dog, it may be any other animal).

3.arrayindexsoutofboundsexception: As the name implies is the array subscript out of bounds error, this kind of problem often appears in the array expansion

(solve array expansion array subscript out of bounds: You can set the array space to large, not allow him to frequent expansion; or maybe? Stacks of Java Virtual machines

space to set a large point) or to traverse the array, do not write less than a value, write less than the array. length so you should pay attention to it.

4.arithmeticexception: algorithm exception, this kind of error is common sense problem the simplest is such as: 3/0= how much, 0 can not do divisor.

5.IllegalArgumentException: Illegal parameter exception, this kind of problem is the parameter type error, Java will clearly tell you where the wrong

(I encountered a mistake in doing login registration, using cookies to do without landing, Cookile is not supported in Chinese

so the cookie must be cookie= new Cookie (Urlencoder.encode (Stu.getusername (), "Utf-8"), Stu.getstu_password ()) to write,

the two inside are username and password, like the user name is the character must be converted characters encoding.

These are common, and of course, heap overflow errors and stack overflow errors, such as the recursion you write too deep or into the dead loop. This kind of question you can look at your logic is not a problem to find

and fix, optimize the code, or you can set the Java virtual machine's memory to set a larger point.

Cough cough. Finally hope to help you a little, if there is any shortage, you can discuss with me, we progress together.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.