Custom Exceptions in Java (standard)

Source: Internet
Author: User

Custom "Invalid name exception"

1. Compile-time exception, directly inherit exception

2. Run-time exception, directly inherit RuntimeException

public class illegalnameexception extends exception{//compile-time exception//public class  illegalnameexception extends runtimeexception{//run-time exception         Public illegalexception () {    }    public illegalexception ( string msg) {        super (msg);     }}// Defines a consumer class Public class customerservice{    public void register (string  name) throws illegalexception{        if (Name.length () <6) {            //exception              //Creating Exception Objects              illegalexception e=new illegalexception ("User name length cannot be less than six bits");             throw e;            //throw  Illegalexception ("User name length cannot be less than six bits");        }         //If executed here, the user name is legal.         system.out.println ("User name is valid, registration is successful!") ");     }}//test class public class test{    public static  Void main (String[] args) {        string name= "Nihao";         customerservice cs=new customerservice ();         try{        cs.register (" Nihao ");         }catch (illegalnameexception e) {              system.out.println (E.getMessage ());         }            }} 


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661718

Custom Exceptions in Java (standard)

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.