5 custom exception

Overview of custom exceptions

When the exception in java can not meet the needs of developers, you can customize the exception.

package com.monkey1024.exception;

public class UserService {
        //Method of registration
        public void register(String name){

            if(name.length()<6){

                //You need an exception to throw an illegal registry name here, but there's no such exception in java
            }

            //If the code can be executed here, the username is valid.
            System.out.println("Successful registration!");

        }
}

Above is a user registration code. If the length of registered user name is less than 6, an exception of illegal registered name needs to be thrown, but there is no exception in java. At this time, developers can customize the exception to meet the requirements.

How to customize exceptions?

See the source code in other Exception s for reference.
If the custom exception is of RuntimeException type, you can inherit RuntimeException directly.
Otherwise, inherit Exception.
After inheritance, two constructors are generally provided, such as the following custom exception named IllegalNameException

package com.monkey1024.exception;

public class IllegalNameException extends Exception{ //Compile-time exception
//public class IllegalNameException extends RuntimeException {// Runtime exception

        //Defining exceptions generally provides two constructive methods
        public IllegalNameException(){}


        public IllegalNameException(String msg){
            super(msg);
        }

    }

Use custom exceptions

After customizing the exception, you can use it. Modify the above code:

package com.monkey1024.exception;

public class UserService {
        //Method of registration
        public void register(String name) throws IllegalNameException{

            if(name.length()<6){

                //You need an exception to throw an illegal registry name here, but there is no such exception in java
                //Manual throw exception
                //Note that throw is not throws
                //Using throw to throw an exception in the method body
                throw new IllegalNameException();
            }

            //If the code can be executed here, the username is validated.
            System.out.println("Successful registration!");

        }
}

As for throw, if something happens within the method and the program can't continue to run, throw the exception object.
To write a test class

public class RegisterTest{

    public static void main(String[] args){

        //If the user provides the following username
        String username = "mk";

        //register
        CustomerService cs = new CustomerService();

        try{
            cs.register(username);
        }catch(IllegalNameException e){
            System.out.println(e.getMessage());
        }


    }
}

The difference between throw and throws

throws
1 is used after the method declaration, followed by the exception class name
2 can be separated from multiple exception class names by commas
3 indicates that an exception is thrown and handled by the caller of the method

throw
1 is used in the method body, followed by the exception object name
2 can only throw an exception object name
3 indicates that an exception is thrown and is handled by the statement in the method body

Tags: Programming Java less

Posted on Thu, 10 Oct 2019 13:24:42 -0700 by Kingy