In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of exception handling in the process of Java development, which is very detailed and has a certain reference value. Interested friends must read it!
1. An exception occurred while running java:
I am here because: arr [3] does not exist:
Java.lang.ArrayIndexOutOfBoundsException: 3
Public class btyf {public static void main (String [] args) {int [] arr= {1mt 2je 3}; System.out.println (arr [0]); System.out.println (arr [3]); System.out.println (arr [1]) / / 1 exception ArrayIndexOutOfBoundsException exception name / / btyf.main (btyf.java:13) exception location Line 13 / Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3Accord / at btyf.main (btyf.java:13)}}
Results:
Java virtual machine: will output abnormal content to the console
two。 Handling exceptions:
Public class btyf {public static void main (String [] args) {int [] arr= {1pr 2pr 3}; System.out.println (arr [0]); try {System.out.println (arr [3]);} catch (ArrayIndexOutOfBoundsException e) {System.out.println ("the array index you accessed does not exist"); e.printStackTrace () / / output exception data: console} System.out.println (arr [1]); / / 1 exception / / ArrayIndexOutOfBoundsException exception name / / btyf.main (btyf.java:13) exception location / / Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3pm / at btyf.main (btyf.java:13)}}
Results:
Catch exceptions through try, and the code without exceptions will not stop because of some exceptions in the previous code.
It can be executed.
3.throwable: member method:
System.out.println (e.toString ()); / / print out the exception: location and name
E.printStackTrace (); / / output exception data: console
System.out.println (e.getMessage ()); same
Multi-purpose: System.out.println (e.toString ()); this
Try {System.out.println (arr [3]);} catch (ArrayIndexOutOfBoundsException e) {/ / System.out.println ("the array index you accessed does not exist"); / / e.printStackTrace (); System.out.println (e.getMessage ()); / / public String getMessage () {/ / return detailMessage; / /} System.out.println (e.toString ());}
Results:
4.throws: throw an exception:
But in the exception: still add try catch
Add location: exception member method
Public static void main (String [] args) throws ArrayIndexOutOfBoundsException {}
Code:
Public class uytig {public static void main (String [] args) throws ArrayIndexOutOfBoundsException {int [] arr= {1Power2) 3}; System.out.println (arr [0]); try {System.out.println (arr [3]);} catch (Exception e) {e.printStackTrace ();} System.out.println ("in progress");}}
The above is all the contents of the article "sample Analysis of exception handling problems in Java Development". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.