Java Exception Exception Handling

First, the definition

An exception (Exception) : refers to the abnormal situation of the program is running, is running a special error object, corresponding to the Java language-specific run-time error handling mechanism.

 

Second, the two common types of exceptions

RuntimeException (a runtime exception ) : including the "wrong type conversion", "array index out of bounds", "array bounds" and "null pointer" and so on, this RuntimeException is often caused due to incorrect procedures;

Non-RuntimeException (non-runtime exception) : mainly "from a file does not exist in the read data," "beyond the end of the file continues to read", "connecting a non-existent URL" etc., this Non-RuntimeException It is usually generated at compile time the compiler, usually caused by environmental factors, rather than the program itself lead to action, such as: IOException.

 

Third, the exception handling in two ways

 

① with a try ...... catch ...... (finally) catch the exception

   try{
           You may encounter an exception
         }

   catch{ Treatment of abnormal
           }

    the finally {} contents of program output

 (Note: ①try alone can not be used in the program, each try block may be accompanied by one or more catch statements may be generated for processing objects of different types of abnormality; ②finally statement provides a uniform exception handling outlet, regardless whether there has been an exception, finally statement in the block will be executed, that finally statement no matter what the output will be abnormal encountered in the try block.)

 

② thrown throws with

  public void readFile (Srting file) throws FileNotFoundException{ }

  // format: + return type method name (parameter list) + throws + exception type list

 

Fourth, illustration

eg: define an integer number 69, it is determined whether the characters in the ASCII code table 'T' are equal, if not equal to 'T' continues executing code output 69 corresponding to the character. IOException may occur in the process of this judgment, we passed this example to compare these two approaches exception handling.

① The try ...... catch ...... (finally) to catch exceptions

Import
java.io.IOException; public class ExceptionTest { public static void Read () { int A = 69 ; the try { the while (( char !) = A 'T') { // is determined corresponding to the character 69 is equal to 'T', and so proceed if Code System.out.println (( char ) A); a = System.in.read (); // read the character from a } } the catch ( IOException E ) { System.out.println ( "ExceptionTest" ); } } public static void main(String[] args) { ExceptionTest.read( ); } }
② with throws an exception is thrown

import java.io.IOException;

   public class ExceptionTest  {
   public static void read( ) throws IOException {
     int a = 69;
    
         the while (( char !) = A 'T') {           // is determined corresponding to the character 69 is equal to 'T', and the like continue to execute code if 
            System.out.println (( char ) A);
            a = System.in.read ();         // read from a character 
            throw  new new IOException ();       // in the method body, does not require additional exception thrown throw s (the exception object and can be omitted 
         }
         
            System.out.println("ExceptionTest");
   }

    
   public static void main(String[] args) throws IOException {
            ExceptionTest.read( );
   }
}

Guess you like

Origin www.cnblogs.com/LittleWhite2019/p/12052336.html