And Java exception handling mechanism

Java's exception hierarchy

All Java exception objects are derived from Throwable class, the lower has two branches: error and exception.

  • Error branch describe internal error or resource exhaustion Java run-time errors encountered derived from the Error of error, the program in addition to notice to the user and try to safely exit outside can not do anything.
  • Exception branch is mainly concerned about the abnormal programmer, it is divided into two branches RuntimeException and IOException.
    1. RuntimeException is usually due to procedural errors.
    2. IOException usual procedure itself is no problem, but as I / O errors and other problems caused.

Java specification Error and RuntimeException branch branches abnormalities known as non-checked exception, other exceptions referred checked exception.

Throw an exception

Programmers write code if the situation encountered (unchecked exception) can not handle, you can choose to throw an exception, the caller will solve the problem to the program.

In the name of the method throws an exception to tell the caller with a statement to declare what the code will throw an exception, throw multiple exceptions For you must list all the exception classes separated by commas. A method must declare all willing to throw a checked exception. E.g:

public void read()throws FileNotFoundException,EOFException

Use the throw statement to throw an exception in a method, for example:

throw new IOException();

Catch the exception

Throwing an exception is an unusual solution, but not the final solution. If the exception handler will not collapse termination, thus requiring catch the exception. Java provides a try / catch statement to catch the exception.

If the try statement abnormality occurs, stops the execution of code in the try block of code is executed instead of the catch block, the capture read exceptions each anomaly takes to process and provide a catch statement from the statement to catch the abnormality to inherit chain from the same parent to the child, may also be combined with a catch phrase '|' combined capture multiple exceptions.

try {
  code
} catch (FileNotFoundException | UnknownHostException e) {
    exception handler
} catch (IOException e){
    exception handler
}

finally statement: If the program is abnormal, it will terminate the code method remaining and exit methods. If the method to get some resources and only method he knows, will have a recycling problem. Java finally clause proposed solutions, no matter how to handle an exception, the finally clause will be executed.

Several code execution sequence when handling exceptions:

try{
    code1
    code that might throw exceptions
    code2
}catch(Exception e){
    code3
    code that might throw exceptions
    code4
}finally{
    code5
}
code6
  1. try block codes without exception, the execution order is 1,2,5,6
  2. Exception code included in the try block, the catch statement, no abnormality occurred, execution order 1,3,4,5,6
  3. Exception code included in the try block, the catch statements also abnormal, 1,3,5 execution order

try statement with resources

For resources to achieve AutoCloseable or Closeable interface, Java provides the try statement with resources, if the try block exits, resources automatically call close () method.

In the form of a try statement (try-with-resources) with resources for:

try(Resource res = ...){
    // code work with res
}

AutoCloseable interface has a close method, it throws an Exception anomaly

void close() throws Exception;

Closeable interface inherits from AutoCloseable interface also has a close method, it throws an IOException

public void close() throws IOException;

Affirmation

When an exception under investigation is typically used in a large number of checks to test the output statements, these statements after the test is completed will still remain in the program, if the program is left a large amount of these checks statement causes the program to run slower.

Assertion mechanism introduced in jdk1.4, which allows the insertion of some checks statements detect anomalies in the code during the development and testing phase. Since the default assertion Java virtual machine is off, and therefore does not affect the operation of the program after the project is released.

Assertion of use

  1. assert condition;
  2. assert condition: expression;

When the condition evaluates to false, the AssertionError throws in the second form to pass into an expression AssertionError class constructor form a message string.

Use -enableassertions or -ea open assertion in the test.

For chestnut:

Scanner in = new Scanner(System.in);
int x = in.nextInt();
assert x>0:x;
double y = Math.sqrt(x);


If the article is wrong place please correct me, we can exchange ideas. Thanks to public concern number JsjCoding

Guess you like

Origin www.cnblogs.com/LeoCoding/p/11220381.html