Java exception handling Extracts

Java exception mechanism depends on the try, catch, finally, throw, throws five keywords.

         1.try: it may throw an exception is placed inside code

         2.catch: a behind the corresponding code block and exception types, indicating that the catch block for processing this type of block, can have multiple catch blocks.

         3.finally: mainly used for material recycling opened in a try block (such as database connections, network connections and disk file), exception mechanism always guarantee that the finally block is always executed. Only finally block, after the execution is complete, it will come back the try or catch block return or throw statement, if finally used in the statement termination method of return or throw, etc., then it will not jump back to perform, direct stop.

         4.throw: for a practical throw exception, may be used alone used as a statement, a specific exception object is thrown.

         5.throws: used in the method signature for this method may throw exception statement.

 

       Java's exception divided into two types, checked abnormality (abnormal compile time) and Runtime exception (a runtime exception)

1. java think checked exceptions are an exception can then compile phase is processed, so it forces the program handles all checked exceptions, while having to deal with abnormal Runtime, java program must explicitly deal with checked exceptions, if the program does not deal with, then the compiler error occurs, it can not compile.

2. checked java anomaly reflects the design philosophy: there is no perfect treatment code will not be executed, reflects the rigor of java,

     For the construction of large, robust, maintainable system for applications, error handling is an important aspect of the entire application to be considered. Java exception handling mechanism, when the program is run accident occurs, the system generates an Exception object to the notification procedure, in order to achieve the "business functions to achieve code" and "error-handling code" separation, providing better readability.

     If an exception occurs in the try block executed business logic code, the system will automatically generate a target exception, the exception object is submitted to the operating environment, a process called throw (the throw) is abnormal. When the Java environment receive an exception object will look for a suitable catch block, if not found, java runtime environment will be terminated, java program exits.

     Different catch block, considered for the different exception class that provides a different approach.

 

For error handling mechanism, mainly has the following two disadvantages:

1. not exhaustive of all exceptions: because human knowledge is limited, exceptions may be better than taking into account the situation much, there is always slip through the net

2. The error-handling code and business code that implements confounding seriously affect the readability, increase the difficulty of program maintenance.

1. Using a try ... catch catch the exception

java proposed a hypothesis, if the program can be completed successfully, then all normal, the system service implementation code in try block is defined, all of the exception processing logic for processing in the catch block.

 

Finally recovered the use of resources

 Sometimes, the program opens the try block inside a number of physical resources (such as database connections, connect the network disk files, etc.), these physical resources must be explicitly recovered.

Because: java garbage collection mechanism will not recover any physical resources, garbage collection only collected heap memory occupied by objects in memory.

the try
{
     // business logic implemented
     ...
}
the catch (SubException E)
{
     // fast exception handling. 1
     ...
}
the catch (SubException2 E)
{
     // fast exception handling 2
     ...
}
     ...
the finally
{
    // recycling blocks
    ...
}

Guess you like

Origin www.cnblogs.com/nxjblog/p/11388380.html