In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Java try catch how to deal with exceptions, the article is very detailed, has a certain reference value, interested friends must read it!
Description:
Public class TryCatchStu {
/ * try catch: handle exceptions by yourself
* try {
* Code that may have an exception
*} catch (exception class name An e) {
* if an exception of type An of the exception class occurs, execute the code
*}. (there can be more than one catch)
* finally {
* the code that must eventually be executed (for example, code that releases resources)
*}
* the order in which the code is executed:
* the code in 1.try interrupts execution starting from the line where the exception occurs
, 2. Execute the code within the corresponding catch block
, 3. Continue to execute the code after the try catch structure
* Note:
* 1. If the exception class in catch has a subparent relationship, then the subclass should come first and the parent class should be last.
, 2. If there is a return statement in finally, the final returned result must be based on the return value in finally.
, 3. If there is a return in the finally statement, the unhandled exception will be swallowed
* Note on rewriting:
* 1. A son can't be more capable than his father.
, 2. A son is more open than his father.
, 3. The son cannot cause more trouble than the father (the exception type of the subclass cannot be the parent type of the exception of the parent class)
* exception class Api:
* 1.getMessage (): get the exception description information string
* 2.toString (): returns the package path and class name of the exception class and the exception description information string
* 3.printStackTrace (): print stack information in addition to printing toString information
, /
Example code: public static void main (String [] args) {System.out.println (demo ());} public static int demo () {GirlHome gh = new GirlHome ("Xiao Chen", "male", 28); try {gh.intoHome (); System.out.println ("entered the girls' dormitory"); return 0;} catch (SexOutException e) {/ / System.out.println ("an exception occurred"); / / System.out.println (e) E.demo (); e.printStackTrace ();} catch (AgeOutException e) {/ / TODO: handle exception} finally {System.out.println ("I must be executed");} return 1; / / System.out.println ("Happy day is over");} class GirlHome {public String name; public char sex; public int age; public GirlHome (String name,char sex,int age) {super (); this.name = name; this.sex = sex This.age = age;} / / if the entrant is found to be male, throw a gender anomaly public void intoHome () throws SexOutException,AgeOutException {if (sexuality 'female') {SexOutException se = new SexOutException (name+ "you're not a woman, get out!"); throw se;} if (age > 25) {throw new AgeOutException (name+ "you're too old, get out!");} System.out.println (name+ "happily entered the dormitory") Public void demo () throws Exception {} class SmallGirlHome extends GirlHome {public SmallGirlHome (String name, char sex, int age) {super (name, sex, age); / / TODO Auto-generated constructor stub} @ Override public void intoHome () throws SexOutException,AgeOutException {} public void demo () {} class AgeOutException extends Exception {public AgeOutException (String message) {super (message);}} class SexOutException extends Exception {public SexOutException (String message) {super (message) } public void demo () {System.out.println ("Allah");}} above is all the content of the article "how to handle exceptions in try catch in Java". 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.