Custom exception. java custom exception

Source: Internet
Author: User

Custom exception. java custom exception

It is estimated that they will no longer play after class, and they will finish the class during that time. This is basically rare, mainly because there is no record of the habit, the strange thing is that every time you get in a bad mood, you always want to write something.

 

In any case, I went out to spend money to learn about this experience. Well, it's hard to evaluate. In fact, if you don't leave a record, there is no learning significance.

 

Therefore, Li Fei needs to make a little pull.

 

When I went out to study, I found that I was really not very smart and could not accept the knowledge as quickly as I did, and my memory was not so good. So what I really want to do if I want to grow up is to work harder and harder.

 

Mr. Yuan Haipeng taught me this day. 10.13 In the impression, because it is 10.12.

 

What we teach today is a custom exception.

 

Steps for customizing exceptions:

1. Create a class. Inherits exception or runtimeException.

2. Writing constructor.

3. An object of this class is created in an invalid state.

Example 1:

package ExceptionPart;public class SelfDefinitionException extends Exception {        public SelfDefinitionException(String message){        super(message);    }        public static void main(String[] args) throws SelfDefinitionException{        throw new SelfDefinitionException("lifei");    }    }

 

 

Running result:

Exception in thread "main" ExceptionPart. SelfDefinitionException: lifei
At ExceptionPart. SelfDefinitionException. main (SelfDefinitionException. java: 10)

 

It seems boring. There is nothing to do. In fact, if you look at this thing separately, it is indeed nothing. In this case, we need to consider what a custom exception is used? The problem is encountered, and then the problem occurs. Avoid situations that are invalid. Report something that is allowed in the compilation environment to the developer in the form of exceptions to better solve the current problem.

For example, when we store a negative number or a large number in the database's age field, an error may occur in an unknown link. If we set the value range of this number at the beginning, the maintenance cost will be very low.

Therefore, the logic is that if a value does not meet the requirements, an exception may occur. So. We do not need to directly report exceptions in the main function. Give a condition.

If (age is unreasonable ){

Throw new AgeException ("invalid age ");

}

According to this logic:

Public class AgeException extends Exception {
Public AgeException (){
Super ("abnormal age ");
}
}

 

So Example 2:

Package ExceptionPart; // age Exception handling class public class AgeException extends Exception {// constructor public AgeException () {// rewrite super (" ");}}

 

Package ExceptionPart; public class AgeExceptionTest {public static void main (String [] args) throws AgeException {// set int age =-5; // detect if (age> 150 | age <0) {throw new AgeException ();} // [Mark, row 9th] // output System. out. println ("my age is" + age );}}

Running result:

Exception in thread "main" ExceptionPart. AgeException: abnormal age
At ExceptionPart. AgeExceptionTest. main (AgeExceptionTest. java: 9)

 

Exercise:

It is legal for men and women to create a custom exception class about gender.

Package ExceptionPart; public class GenderException extends Exception {public GenderException () {super ("the entered gender does not comply with the specification");} public GenderException (String message) {super (message );}}
Package ExceptionPart; public class GenderExceptionTest {public static void main (String [] args) throws GenderException {String gender = "s"; if (! "Male". equals (gender )&&! "Female ". equals (gender) {throw new GenderException ("gender input does not comply with the specifications");} System. out. println ("James's gender is:" + gender );}}

 

 

 

Problem:

There is a problem: I don't know how to solve it.

I hope to discuss it with my friends:

Problem Source: an overloaded function of the current class is expected to be written. Then, the age value is used to determine which statement is used for output.

However, this exception occurs.

Package ExceptionPart; public class AgeException1 extends Exception {private final String NATIVETIPS = "the current age is negative"; private final String BIGTIPS = "the current age is too large "; private static String result = "your age is illegal"; public AgeException1 (int age) {if (age <0) {result = NATIVETIPS;} else if (age> 150) {result = BIGTIPS;} System. out. println (age); System. out. println (result); super (result); // here the compiler will report an error} public AgeException1 () {super (result );}}

Change method:

Package ExceptionPart; public class AgeException1 extends Exception {private final String NATIVETIPS = "the current age is negative"; private final String BIGTIPS = "the current age is too large "; private static String result = "your age is invalid"; public AgeException1 (int age) {super (result); // No problem if you move it here. If (age <0) {result = NATIVETIPS;} else if (age> 150) {result = BIGTIPS;} System. out. println (age); System. out. println (result); // you can get the sentence you just mentioned.} public AgeException1 () {super (result );}}

Then the compilation error is:

Constructor call must be the first statement in a constructor

The conjecture should be translated into: constructor calls must first be declared in a constructor.

When the current class is created, the constructor of the parent class is preferentially called. So if you don't write it, you can only write it in the first line. And then give priority to the call. If this parameter is left blank, the system takes priority and is called by default.

 

 

If you have any questions, please feel free to discuss them. BY Letben.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.