1 /*2 Exception: The program has an unhealthy situation. 3 4 Example: Today the weather is very good, the monitor goes out to travel. Ride a bike, go to the mountains and breathe the fresh air. 5 Error:6 Question 1: The mountain road collapsed, the monitor stopped in time, but couldn't get through. A serious problem. (Error, compilation cannot be processed)7 Exception:8 Question 2: The monitor goes out to push the bicycle, discovers the gas is gone, blows up the air. Problems that should be checked before departure. (Non-runtimeexception exception, must be handled)9 Question 3: The Monitor rides the car on the mountain road comfortable driving, the road on both sides are small stones, the middle is flat cement road. (RuntimeException need to fix code)Ten always on the flat cement road is no problem, but he likes to ride on the small stone, the result of a puncture. The problems that arise in the course of tourism. One no Zuo no die. A - exception to program: Throwable - Critical Issue: Error is a critical error that cannot be recovered by the program itself and cannot be processed the problem: Exception represents an exception error that can be caught and handled by the program - compile time problem: The exception that is not runtimeexception must be handled, because you do not process, the compilation cannot pass. - Run-time issue: RuntimeException The problem is definitely that our code is not rigorous enough to fix the code . - + There is a problem with the program, we do not do any processing, and eventually the JVM will make the default processing. - The name of the exception, the cause of the problem and the occurrence of information output in the console. + The program is also closed. A at How do we handle exceptions ourselves? - a:try...catch...finally - B:throws throws - - processing format for try...catch...finally: - try { in code that may be problematic; - }catch (Exception name variable) { to deal with the problem; + }finally { - releasing resources; the } * $ morph Format:Panax Notoginseng try { - code that may be problematic; the }catch (Exception name variable) { + deal with the problem; A } the + Note: - the less code is better in A:try (because the JVM opens up additional space for the code inside the try, so less is better, save space) $ B:catch must have content inside, even give a simple hint (otherwise it is not to deal with the problem, but to hide the problem) $ - A : An exception -B: Two exception handling the A : Write a try...catch for each one - B: Write a try, multiple catchWuyi try{ the ... - }catch (Exception class name variable name) { Wu ... - } About catch (Exception class name variable name) { $ ... - } - ... - A Precautions : + 1: Can clear as far as possible clear, do not use the big to deal with. the 2: The exception of the relationship between the peer who before who does not matter, if a child parent relationship, the parent must be in the back. - $ Note: the Once something goes wrong in the try, it throws the problem here and matches the problem in the catch. the Once there is a match, execute the handle inside the catch and then end the Try...catch the continue execution of the following statement. the */ - in Public classExceptionDemo2 { the Public Static voidMain (string[] args) { the //method1 (); About the //method2 (); the the //method3 (); + - method4 (); the }Bayi the Public Static voidmethod4 () { the intA = 10; - intb = 0; - int[] arr = {1, 2, 3 }; the the //Grandpa at the end the Try { theSystem.out.println (A/b); -System.out.println (arr[3]); theSystem.out.println ("There's an anomaly here, you don't know who it is, what should I do?")); the}Catch(ArithmeticException e) { theSYSTEM.OUT.PRINTLN ("Divisor cannot be 0");94}Catch(arrayindexoutofboundsexception e) { theSYSTEM.OUT.PRINTLN ("You have access to an index that should not be accessed"); the}Catch(Exception e) { theSystem.out.println ("Something's Wrong");98 } About - //Grandpa is not allowed in front.101 //try {102 //System.out.println (A/b);103 //System.out.println (arr[3]);104 //System.out.println ("There's an anomaly here, you don't know who it is, what to do?"); the //} catch (Exception e) {106 //System.out.println ("problem");107 //} catch (ArithmeticException e) {108 //System.out.println ("divisor cannot be 0");109 //} catch (ArrayIndexOutOfBoundsException e) { the //System.out.println ("You visited the index of the wrong Access");111 // } the 113System.out.println ("Over"); the } the the //handling of two exceptions117 Public Static voidmethod3 () {118 intA = 10;119 intb = 0; - int[] arr = {1, 2, 3 };121 122 Try {123System.out.println (arr[3]);124System.out.println (A/b); the //System.out.println (arr[3]);126}Catch(ArithmeticException e) {127SYSTEM.OUT.PRINTLN ("Divisor cannot be 0"); -}Catch(arrayindexoutofboundsexception e) {129SYSTEM.OUT.PRINTLN ("You have access to an index that should not be accessed"); the }131 theSystem.out.println ("Over");133 }134 135 //Two exceptions136 Public Static voidmethod2 () {137 intA = 10;138 intb = 0;139 Try { $System.out.println (A/b);141}Catch(ArithmeticException e) {142SYSTEM.OUT.PRINTLN ("Divisor cannot be 0");143 }144 145 int[] arr = {1, 2, 3 };146 Try {147System.out.println (arr[3]);148}Catch(arrayindexoutofboundsexception e) {149SYSTEM.OUT.PRINTLN ("You have access to an index that should not be accessed"); Max }151 theSystem.out.println ("Over");153 }154 155 //an exception156 Public Static voidmethod1 () {157 //First Stage158 intA = 10;159 //int b = 2; the intb = 0;161 162 Try {163System.out.println (A/b);164}Catch(ArithmeticException ae) {165SYSTEM.OUT.PRINTLN ("Divisor cannot be 0");166 }167 168 //Phase II169System.out.println ("Over"); the }171}
Java 19-2 Exceptions and methods of Tr...catch () handling Exceptions