Java throw: throw an exception

So far, you just get a Java runtime system is thrown. However, the program can be thrown clear exception with the throw statement. Common forms Throw statement is as follows:
the throw ThrowableInstance;
here, ThrowableInstance must be an object type or class Throwable subclass of Throwable type. Simple type, such as int or char, and a non-class Throwable, e.g. String or Object, abnormality can not be used. There are two methods Throwable object can be obtained: in the catch clause parameters or create new operator.

Program execution stops immediately after the throw statement; any statement after being executed. The most closely surrounding try block is used to check whether it contains a type of abnormality matching catch clause. If the matching block found, control passes to the statement; If no, the try block enclosed times to check, and so on. If no matching catch block, the default exception handler interrupt program execution and print stack trace.

Here is an example of creating and thrown program, and the handler matches the exception handler then throws it to the outer layer.
The Demonstrate the throw //.
Class ThrowDemo {
static void demoproc () {
the try {
the throw a NullPointerException new new ( "Demo");
} the catch (a NullPointerException E) {
System.out.println ( "Caught Inside demoproc.");
The throw E; / / The Exception the rethrow
}
}

public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println(“Recaught: ” + e);
}
}
}

The program has two opportunities to deal with the same error. First of all, main () established an exceptional relationship and then call demoproc (). demoproc () method then established relationship and another exception handler immediately NullPointerException is thrown a new instance, the next line NullPointerException is captured. So an exception is thrown again. Here is the output:
Caught Inside demoproc.
Recaught: java.lang.NullPointerException: Demo

The program also describes how to create a standard Java exception object, with particular attention the following line:
the throw new new NullPointerException ( "Demo");
here, new to construct a NullPointerException instance. All Java exception has built-in run-time two constructors: one with no parameters, a parameter with a string. When the second form is used, the parameter specifies the string that describes the exception. If the object is used as print () or println () parameters, the string is displayed. The same can be done by calling the getMessage (), getMessage () is defined by the Throwable.

VII. Multithreaded programming
1. The concept of threads
2. the Java threading model
3. The main thread
4. Create a thread
5. Create a multithreaded
6. Use isAlive () and join () of
7. thread priority
8. thread synchronization
9. communication between the threads
10. a thread deadlock
11. the thread suspend, resume and terminate

Guess you like

Origin blog.csdn.net/Javaxuxuexi/article/details/92428302