In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "Java exception handling operation case analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The use of exception handling-the ability to catch exceptions
1 code
Public class DealException {public static void main (String [] args) {try / / check the code of this block {int arr [] = new int [5]; arr [10] = 7; / / an exception will occur here} catch (ArrayIndexOutOfBoundsException e) {System.out.println ("Array out of bind!" );} finally / / the program code of this block must execute {System.out.println ("it must be executed here!" );} System.out.println ("main () method ends!" );}}
2 run
Array is out of bind range! This place will be executed! The main () method ends!
Second, the use of exception handling-the exception cannot be caught
1 code
Public class DealException {public static void main (String [] args) {try / / check the code of this block {int arr [] = new int [5]; arr [10] = 7; / / an exception will occur here} catch (ArithmeticException e) {System.out.println ("arithmetic exception");} finally / / the program code of this block must execute {System.out.println ("it must be executed here!" );} System.out.println ("main () method ends!" );}}
2 run
This place will be executed! Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at DealException.main (DealException.java:9)
3 description
For the uncaught exception scenario, the finally statement block will be executed, but the statement after the finally language block cannot be executed, so the exception will be handed over to JVM to handle, and finally stop the program from running.
The use of three exception objects ex
1 code
Public class excepObject {public static void main (String [] args) {try {int arr [] = new int [5]; arr [10] = 7;} catch (ArrayIndexOutOfBoundsException ex) {System.out.println ("the array is out of range!" ); System.out.println ("exception:" + ex); / / display the contents of the exception object e / / ex.printStackTrace ();} System.out.println ("main () method ends!" );}}
2 run
Array is out of bind range! Exception: end of java.lang.ArrayIndexOutOfBoundsException: 10main () method!
Fourth, catch exceptions through multiple catch
1 code
Public class arrayException {public static void main (String args []) {System.out.println ("- A, before calculation starts"); try {int arr [] = new int [5]; arr [0] = 3; arr [1] = 6; arr [1] = 0; / / Divisor 0, exception arr [10] = 7; / / Array subscript crosses the bounds with exception int result = arr [0] / arr [1] System.out.println ("- B, division result:" + result);} catch (ArithmeticException ex) {ex.printStackTrace ();} catch (ArrayIndexOutOfBoundsException ex) {ex.printStackTrace ();} finally {System.out.println ("- this will be executed regardless of whether there is an error or not!") ;} System.out.println ("- C, after the calculation is over.") ;}}
2 run
-A, java.lang.ArrayIndexOutOfBoundsException: 10 at arrayException.main (arrayException.java:19) before the calculation starts-regardless of whether there is an error or not, it will be executed here! -C. after the calculation is over.
This is the end of the content of "Java exception handling operation example analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.