Exception Handling, common exception descriptions, and Exception Handling

Source: Internet
Author: User

Exception Handling, common exception descriptions, and Exception Handling

Common exceptions:

① Java. lang. ArithmeticException counts abnormally. It is thrown when an arithmetic error (for example, Division by zero) occurs during running.

 

② A null pointer exception occurs in java. lang. NullPointerException. Thrown when calling attributes and methods for null values.

 

③ The subscript of the Java. long. ArrayIndexOutOfBoundsException array is out of bounds. Thrown when the array variable reference is invalid.

 

④ An error occurred while formatting the Java. long. NuberFormatException number. When a string is converted to an integer, it contains non-numeric characters.

 

⑤ No exception was found in the java. io. FileNotFoundException file. When a file is operated, it is thrown when the file path is incorrectly written.

 

⑥ Java. SQL. SQLException: Column "id2" not found Column name writing error

 

7. java. SQL. SQLException: the number of Parameter index cut of range placeholders does not match the number of parameters

 

Invalid com. mysql. jdbc. exceptions. jdbc4.MysqlSyntaxErrorException you have an error in your SQL syntax SQL statement writing error

 

Exception Handling: capture exceptions

 1 try { 2  3  4  5 }   catch (Exception e) { 6  7          e.printStackTrace()  8  9 }10        finally{11 12          System.out.println("000000000");13 14 }15 16  

Code Description:

The Try block describes the code to be tested, that is, the code that may throw an exception. Catch the exception object thrown in the try block through the exception variable e. When an exception is thrown in the try block, the code in the catch block will be executed.

② If an exception is thrown during try. The code running after the try block is terminated and transferred to the catch Block for execution.

③ If a try block contains multiple catch Block catch exceptions, when a try block throws an exception, only the first catch block matching the exception will be executed, and all other catch blocks will be ignored. (In multiple catch blocks, exceptions of the parent class are usually written below .)

Finally{

System. out. println ("000000000 ");

}

Finally block:

Finally block indicates the statement block that will be executed no matter whether there is an exception or not. We usually use finally to describe the action of resource recycling, such as closing the stream, closing the database, and so on.

 

If there is a return statement in the try block or catch block, the finally block will be executed before the method jumps out.

If the Virtual Machine (System. exit (0) is disabled in the try block, finally will not be executed soon.

 

Abnormal execution sequence:

First, execute try blocks in sequence. If an exception occurs, transfer the catch Block and terminate other code in the try block. Whether an exception exists or not, the finally statement block is executed.

 

Throw NewException (); indicates that an Exception object is thrown.

 

Exception category:

1. Exception during compilation: capture exceptions are required. If exception capture is not performed, a compilation error occurs. Therefore, a compilation exception is also called a check exception.

2. runtime exceptions: developers can capture or not capture exceptions. If not captured, no compilation error will occur. All runtime exceptions are subclasses of RuntimeException. Therefore, runtime exceptions are also called non-checking exceptions.

 

Throws is the method modifier. Indicates that a method may throw an exception. When calling this method, you must capture the exceptions thrown by this method.

 

 

 

 

 

 

 

 

 

// Obtain the number of milliseconds from January 1, January 1, 1970 to the current time.

Long x = System. currentMillis ();

System. out. println (x );

Garbage collection conditions:

1. The object is not referred to by the variable.

2. This object is not used

Garbage collection is controlled by virtual machines and cannot be intervened by programmers, but it can speed up garbage collection through System. gc.

 

// Obtain the current working directory of the user

String path = System. getProperty ("user. dir ");

System. out. println (path );

 

Date-related classes:

@ DeprecatedFor method annotations, it indicates that this method is outdated and can be used, but there are better ways to replace this method.

 

Date d = new Date ();

// Obtain the year

Int year = d. getYear ();

// Obtain the month

Int month = d. getMonth ();

// Get the date

Int date = d. getDate ();

 

System. out. println (1900 + year ));

 

Package

Parent class

Class

Method

Remarks

Java. util

 

Date

 

 

Java. long

 

Math

Random ()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

String

 

Final type, not subclass

 

 

 

 

 

 

 

Packaging class: The value contained in the packaging class object remains unchanged.

 

 

String a ="Abc"; And String B = new String ("Abc"); Difference:

Variable a points to the String constant object in the data segment. Variable B points to a string object in the heap space. However, the string constants stored in the heap space must use string constants as the template and be merged into the heap space. Therefore, a String object created using new String ("abc") may generate two String objects. One is in the Data Segment and the other is in the heap.

 

The subclass overrides the method of the parent class. The method name, return type, and parameter list must be a value. The requirement is that the access modifier must be greater than or equal to the parent class method. The thrown exception must be less than or equal to the parent class method.

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.