/ * Custom exception class Exception class java provided, enough for us to use, need to define some unusual class format: public class XXXException the extends Exception / RuntimeException { add constructor an empty parameter adding a constructor exception information with } Note: 1. custom exception class generally end exception, such description is an exception class 2 must inherit exception / a RuntimeException * / public class RegisterException the extends exception { // add an exception with an empty argument public RegisterException () { } // View source abnormality known, allows the parent class constructor to solve the exception information public RegisterException (the message String) { Super (the message); } }
. 1 Import ; java.util.Scanner 2 . 3 / ** . 4 * abnormal use, simulation registration function . 5 * / . 6 . 7 public class IsRegister { . 8 // username (database) storage array 1 has been registered . 9 static String [] usename = { "Zhao", "Wang", "Sun" }; 10 . 11 public static void main (String [] args) / * throws RegisterException * / { 12 is // 2. Usually source data acquired from the distal end 13 is Scanner SC = new new Scanner (the System.in); 14 System.out.println ( "Please enter your name:" ); 15 String name = sc.nextLine (); 16 checkUsename (name); . 17 18 is } . 19 // 3. define a method for determining the data entered by the user 20 public static void checkUsename (String name) / * throws RegisterException * / { 21 is for (String usename: usename) { 22 is IF (usename.equals (name)) { 23 is the try { 24 the throw new new RegisterException ( "registered user"); // runtime exception, the catch can not throws and the try 25 } The catch (RegisterException E) { 26 is e.printStackTrace (); 27 return ; // End 28 } 29 } 30 } 31 // successfully registered 32 System.out.println ( "registered successfully" ); 33 } 34 }
/ * // all exceptions Throwable superclass Exception - compile an exception, or throws can try {} catch {} - subclass RuntimeException - abnormal operation, can not handle, let the JVM handle Error - can not handle, you must modify the source code, memory overflow, system crashes - subclass exception handling: method inside an exception, see if there is exception handling logic, if not - the content, the reason, the location of the caller reported JVM-- find methods (main) - see if there is abnormal processing logic, if not - return to the JVM, JVM console print, and interrupt program abnormal // runtime throw key role: You can use the keyword throw throw exceptions specified in the method specified in the format: throw new xxxException ( "the reason abnormalities resulting") Note: 1.throw keyword must be inside the process 2.throw keyword must be new or exception subclass object 3.throw keyword specified exception object is thrown, we have to deal with this Object 1.throw keyword is behind the creation of RuntimeException or a subclass object, to JVM (in print anomalies + Program) 2.throw behind the creation of the keyword is compiled abnormal (write the code), or throws, or try catch work, must be passed over to the other methods of checking legalization, illegal, way to throw an exception is to the caller // compile an exception throws keyword: the first way exception handling, was to deal with the role: when the internal method throws an exception object, then we must deal with this exception object throws the exception object declaration (content, cause, location) to the caller, it does not deal with, will eventually be delivered to the JVM format: modifier return type method name (parameter list) throws XXXException, XXX, XXX { the throw new new XXXException ( "cause of the exception.") } Note: exception statement must be behind throws exception or a subclass internal method if there are a plurality of the exception object, a plurality of throws must be declared directly declare parent exception if the exception object to the parent object has child Further, once declared such as method throws an exception, it must handle the exception, or continue the call Like throws a statement exception to the method last last caller to the JVM, interrupt or try ... catch their own treatment (usually this is better) without interruption try catch: A second embodiment of the exception handling, processing their format: try { possible exception code } catch (Exception define a variable try receiving abnormal variable) { exception handling logic will work into the log information recording } .. . catch (exception define a variable try receiving abnormal variable) { exception handling logic will work into the log information recording } Note: try there may be exceptions to the object, it is necessary to process the plurality of catch if abnormality occurs in the try will perform exception processing logic catch, the code continues after the execution if the catch in the exception class parent-child relationship classes, subclasses should be written in the upper (ambiguity), exception all exception classes finally format: try { possible anomalies code } catch (exception define a variable try receiving abnormal variable) { exception handling logic will work into the log information recording } ... the catch (exception define a variable try receiving abnormal variable) { Exception handling logic will work into the log information recording } finally { regardless of the code is abnormal, there are executed } Note: finally can not be used alone, must be used with the try, finally releasing resources for general, regardless of whether there is an abnormality procedures , are required to release resources * / Import java.io.FileNotFoundException; Import java.io.IOException; Import java.util.List; public class the Throwable { public static void main (String [] args) / * throws IOException * / { / / a RuntimeException can not deal with their own to the JVM // int [] NUM = new new int [. 5]; // getElement (NUM,. 6); //Compile an exception, it needs its own treatment, using throws // readFile ( "c: \\ b.txt"); is not executed because the above abnormal JVM let the program has been interrupted // use the catch the try the try { readFileC ( "c: \ \ b.txt "); @ possible abnormal code } the catch (an ArrayIndexOutOfBoundsException E) { // What throws exception statement, it is used to generate the object reception @ exception processing logic / * the Throwable class defines three method a: string toString () - returns detailed message string of this throwable. StackTraceElement [] getStackTrace () - Provides programmatic access by the printStackTrace () to the stack trace information, JVM default print This method is called String getMessage () - Returns the short description of this throwable. * / System.out.println (e.toString ()); // java.lang.ArrayIndexOutOfBoundsException: Index of bounds for length. 4. 3 OUT System.out.println (e.getMessage ()); // Index of bounds for OUT. 4 . 3 length System.out.println (e.getStackTrace ()); // [Ljava.lang.StackTraceElement; @ 4e50df2e } the finally { System.out.println ( "release resource"); // the finally whether abnormality are performed } System.out.println ( "labor is not interrupted too strong" ); } public static void getElement ( int [] arr, intindex) { // runtime exceptions, can not handle their own IF (index> 4 || index <0 ) { the throw new new ArrayIndexOutOfBoundsException ( "array index in question"); // ArrayIndexOutOfBoundsException runtime exception, to the JVM, do not have to the throw } // have a NullPointerException determination method can determine whether the object is null simplify objects in the above code // format Objects.requireNonNull (object name); // other overload Objects.requireNonNull (object name, "throws information ") } / * define a method, the path of the file transfer legalized determination using throws since the extends IOException a FileNotFoundException IOException statement can therefore * / public static voidreadFile (String the src) throws IOException { // throw an exception file location IF (! src.equals ( "C: \\ a.txt" )) { the throw new new a FileNotFoundException ( "position does not match File" ); } // throw an exception file suffix IF (! src.endsWith (. "TXT" )) { the throw new new IOException ( "position does not match file" ); } } public static void readFileC (String the src) { List <Integer> = integers List.of (. 1, 2,. 3 ); integers.get ( . 4 ); } }