Summary of Common exceptions in JAVA Basic Testing

Source: Internet
Author: User

1. The first JAVA routine: hello world

Problem during execution: problems Executing Hello

If you see the following error message:

Java is not recognized as an internal or external command ,...
Or
Java: Command not found.

You may not have installed java or set the path javain directory in the environment variable.

Check whether your java installation and environment variables are correctly set.

If you see:

Exception in thread "main" java. lang. NoClassDefFoundError: hello

(Wrong name: Hello )...

It is very likely that you did not pay attention to the case or wrong letters in the name. For example:

Java hello

Enter the correct command and run it again.

If you see:

Exception in thread "main" java. lang. NoClassDefFoundError: Hello

/Class

You need to consider whether to remove the. class section from the name.


2. Analyze the exceptions in java. lang. NoClassDefFoundError.

This problem has occurred in the first debugging routine. But what if the name we entered is correct and the problem still persists?

JDK1.4.0 is installed, and JAVA_HOME, PATH, and CLASSPATH are all set. As follows:

JAVA_HOME =/home/jdk

CLASSPATH = $ JAVA_HOME/jre/lib/tools. jar: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/jre/lib/: $ JAVA_HOME/lib

PATH = $ PATH: $ JAVA_HOME/bin

Hello. class is generated using the JAVAC command. When java hello is used, the following message is still displayed:

Exception in thread "main" java. lang. NoClassDefFoundError: XXX (wrong name: XXX)

Solution prompt:

(1) Add (current directory) to your classpath or class definition Add public

The statement is consistent, but an error occurs during execution in this directory.

Method:

.

(3) If you use packet

Check that you have compiled a java file that does not include the specified package in your file.

3. Common exception capture and handling:

A brief description of several basic exceptions in JAVA:

ArithmeticException is generated when an exceptional arithmetic condition occurs.

NullPointerException is generated when an application attempts to use an empty object.

ArrayIndexOutOfBoundsException is generated when the subscript of the array is out of bounds.

ArrayStoreException is generated when the program attempts to store incorrect data types in the array.

FileNotFoundException is generated when the file to be accessed does not exist.

IOException is caused by a general I/O fault, such as a file read failure.

NumberFormatException is generated when a string fails to be converted to numeric data.

OutOfMemoryException is generated when the memory is insufficient.

SecurityException occurs when a small application (Applet) tries to execute an action that is not allowed due to browser security settings.

StackOverflowException is generated when the system's stack space is used up

StringIndexOutOfBoundsException

The above exceptions are basically the exceptions we may encounter in JAVA.

Throwable has two direct subclasses: Error and Exception. An Exception class object is an object processed or discarded by a Java program. Java provides two Exception modes: The Runtime Exception generated during execution and the Checked Exception ). All Checked exceptions are from java. lang. exception, while Runtime Exception inherits java. lang. runtimeException or java. lang. error (actually java. lang. the last layer of RuntimeException is java. lang. exception ). Different subclasses correspond to different types of exceptions. RuntimeException indicates the exception generated by the Java Virtual Machine at runtime.

In terms of the operating mechanism of the program, the Runtime Exception is different from the Checked Exception. Logically, the Runtime Exception and Checked Exception are used for different purposes.

In general, Checked Exception indicates that the Exception must be processed. That is to say, the program designer should already know that it may receive an Exception (because try to catch it ), therefore, the program designer should be able to handle different Checked exceptions.

However, a Runtime Exception usually implies a program error, which may cause the program designer to be unable to handle and cause the program to fail to execute.

Java's controllable exception handling is implemented through five keywords: try, catch, throw, throws, and finally. The following keywords are explained in JB's online help:

Throws: Lists the exceptions a method cocould throw.

Throw: Transfers control of the method to the exception handler.

Try: Opening exception-handling statement.

Catch: Captures the exception.

Finally: Runs its code before terminating the program.

· Try statement

The try statement specifies a piece of code with braces {}, which may discard one or more exceptions.

· Catch statements

The parameters of a catch statement are similar to the declaration of a method, including an exception type and an exception object. The exception type must be a subclass of the Throwable class, which specifies the exception type processed by the catch statement. The exception object is generated and captured by the runtime system in the code block specified by try, the braces contain the processing of objects, and the methods of objects can be called.

There can be multiple catch statements to handle exceptions of different classes. During Java runtime, the system detects the exception types processed by each catch statement from top to bottom until a catch statement with the matching types is found. Here, type matching means that the exception type processed by catch is exactly the same as the type of the generated exception object or its parent class. Therefore, the sort order of catch statements should be from special to general.

You can also use a catch statement to process multiple exception types. In this case, its exception type parameter should be the parent class of these multiple exception types, in the program design, You must select the exception handling type of the catch statement according to the specific situation.

· Finally statement

In the Code defined by try, when an exception is discarded, the subsequent code will not be executed. You can use finally statements to specify a piece of code. No matter whether the exception type of the catch statement is the same as that of the discarded exception type in the block specified by try, the Code specified by finally must be executed, it provides a uniform exit. You can usually clear resources in finally statements. For example, close open files. The finally statement is executed no matter whether the exception occurs or not.

Related Article

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.