In Java try-catch-finally exception handling

In Java try-catch-finally exception handling

First, exception handling

An exception (Exception): is not operating properly happens.

Original exception handling:

if (condition) 
{ 
  approach 1 
  approach 2 
  approaches. 3 
} 
if (condition) 
{ 
  approach 4 
  approach 5 
  approaches 6 
}

  Code reading is poor, bloated, very close to the normal flow of the code combination, so a series of improvements in JAVA, a series of common problems, an object-oriented way of thinking, they are described in the package.

class approach 
{ 
    approach 1 () 
    { 
    Example 1 
    } 
    approach 2 () 
    { 
    Example 2 
    } 
} 
IF (wrong) 
{ 
  throw new approach 1 (); This method may include the name of the problem, information, time, locations. 
}

  

In JAVA, the form of the irregularities of the class described and an encapsulated object. When problems with the program, call the appropriate approach.

Description irregularities class, called exception class. The process code and the exception code separation.

JAVA is abnormal through object-oriented thinking, the problem will become encapsulated object. It is described with exception class. Different problems, different classes are described. It means, how much of a problem, like there are that many.

Second, abnormal system

A lot of problems, described the class means a lot, it will be drawn up in common, on the formation of abnormal system. The final disorders fall into two categories :

Throwable (parent): problems, you should throw, let the caller handle. It features of the system lies in its Throwable subclass of having disposable.

  Realization of two keywords can throw: throws, throw

1. General not treated. Error (error)

  Features: It is the severity of the problem by the JVM (java virtual machine) thrown. This problem occurs, treatment is generally not targeted directly modify the program.

2. can handle. Exception (abnormal)

  Features: extension subclass are as a suffix, read with strong parent class name.

Three exception - Principle & exception object thrown throw

static void main public (String [] args) 
{ 
    block exception throw new method (); 
} 
the throw as a keyword, specifically for an operation exception is thrown. 
 
Chinese abnormality information is defined (for example): 
the throw new new method of abnormality (abnormality general method which can be placed in a character string String type, where the Chinese writing prompted to OK);

  

As can be seen, abnormal, direct call to throw an exception underlying method throws an exception, but this is done at the bottom, we can not see it.

It has a JAVA virtual machine exception handling mechanism is abnormal will report out a variety of information, such as location, for resolve exceptions.

Real development, these anomalies are not directly reported out, it will be saved as logs, we regularly see. And this exception to the user information is useless, we only have to use.

Fourth, the exception - custom exception class thrown throws an exception &

Custom anomaly: a bunch of existing exception JAVA given no we need, this time can be customized. But this class must inherit Exception class .

This is a custom exception, need to inherit Exception class 
class Demo the extends Exception 
{ 
    abnormality Constructor Well 
    Demo () 
    { 
    } 
    Demo (int A) 
    { 
    Super (A); this is a method of the parent class Throwable written, inherited Exception is the parent class Throwable 
    } 
     Demo (String B) 
    { 
    Super (B); 
    } 
} 
 
class Demo1 
{ 
    public static void main (String [] args) throws throws Demo Note that this is thrown to the declared method in anomaly, the direct write back on the line 
    { 
    the throw new new Demo (here write your Chinese anomalies prompt); 
    } 
}

  

V. anomaly - the difference between abnormal difference & throw and throws detect abnormal compile time and run

Exception system in two ways: 1. A compile-time abnormality is detected (throws). In addition to all subclasses runtimeException subclass. Such problems can be targeted treatment.

An exception (throw) 2. runtime. Exception subclass in runtimeException and its subclasses. This problem is generally not processed directly by the compiler, so that when the program is invoked at runtime forced to stop.

Sixth, abnormal - abnormal catch try-catch

Exception handling form capture: specific format:

try try 
{ 
  needs to be detected abnormal code 
  do not try to put all hell does not require testing do not put 
} 
the catch (exception class variables) - This variable receives a special target of the current exception occurred 
{ 
  the code that handles the problem solved the program continues 
} 
the finally finalize 
{ 
  will code executed 
}

  

Seven exception - the case of multi-catch

attempts to try 
{ 
  to be detected abnormality code 
} 
the catch (Exception class variables) 
{ 
  code that handles 
} 
the catch (Exception class variables) 
{ 
  code that handles 
} 
the finally finalize 
{ 
  will code executed 
}

  

Corresponding to a plurality of time try to catch small details:

When there is need for a multi-catch catch (Exception e) the need to put the final, or will hang, because the parent Exception class can receive all exceptions, then put it on the other superfluous, so it should be placed the final catch.

Eight exception - Exception Handling Principles

An exception is the problem, JAVA some common problems have been somehow, and make use enough.

If an individual problem only in your own projects appeared, and JAVA there are no such problems, it would need to describe the problem themselves.

1. If an exception is thrown within the required detection method, then the method must be stated otherwise have to catch with try-catch in the method, otherwise fail to compile.

2. If you call a statement of abnormal function, or try-catch or throws, otherwise fail to compile.

3. When catch, when you throw? Content can be solved with a catch, can not be resolved, with throws tell the caller, the caller has to solve.

4. If a function throws a number of exceptions, then there must be a corresponding catch more targeted treatment when calling.

Nine, block abnormal -finally

finally the code will be executed, only one case, finally not performed.

the try 
{ 
detection code 
} 
the catch 
{ 
  return; exits the overall process, but finally still performed. 
  System.exit (0); exit the java virtual machine, only this case finally not performed. 
} 
The finally closed commonly used (release) resources 
{ 
  inter VM exit outer case, regardless of loud noise, are performed. 
} 
System.out.println ( "the catch in written return, the code will not be executed outside finally, to be exact, out of the whole is the direct method");

  

try-catch-finally block combination of features:

1.try-catch-finally common combination

2.try-catch (can be multiple catch) did not finally, there is no need to release resources (closed), you can not finally.

3.try-finally, there is no catch, throws next method requires a statement, because no catch no treatment. Abnormal can not directly handle catch, but resources need to be closed, then use this combination.

Ten, unusual precautions

1. When a subclass overrides the parent class method, if the parent class of the exception is thrown, then the method can throw an exception subclass or the subclass exception of the parent class.

2. If the parent class throw multiple exceptions, then the subclass can throw only a subset of the parent class exception. ---- subclass overrides the parent class can only throw an exception parent class or sub-class or subset. If the parent's method does not throw an exception, then when the subclass covering must not throw, you can only try.

 

Common Anomaly:

Error class of common subclasses:

 Exception class of common subclasses:

RuntimeException class of common sub-categories:

 

 

 

 

Guess you like

Origin www.cnblogs.com/csushl/p/11973468.html