Java Serial 65-A Preliminary Study of Custom Manual Throwing Exceptions, Exception Ranges of Subclasses, Arrays

Keywords: Java github less Attribute

1. Manually throw an exception 1. Custom invalid name exception:

(1) Compile-time exceptions, inheriting Exception directly

(2) Runtime exceptions, inheriting RuntimeException directly

Example: Note: throws throws an exception up, if it runs to the top, that is, to the main main method, don't throw it anymore, use try...catch.... To print it out, of course, writing throws does not seem to be a problem.

 

package com.bjpowernode.java_learning;

​

public class D64_2_ManualThrowException {

  public static void main(String[] args) {

    try {

      Customer65 c1 = new Customer65();

      c1.register("ng");

    }catch(IllegalNameException i1) {

      System.out.println(i1.getMessage());

    }

  }

}

class IllegalNameException extends Exception{

  //Compile-time exceptions

  public IllegalNameException() {}

  public IllegalNameException(String msg) {

    super(msg);

  }

}

class Customer65{

  String username = "Jack";

  public void register(String username) throws IllegalNameException{

    if(username.length()<3) {

      //Create Exception Object

      IllegalNameException i1 = new IllegalNameException("User name length cannot be less than 3 digits");

      //Manually throw an exception

      throw i1;

     

    }else {

      this.username = username;

    }

  }

 

}

2. Overridden methods cannot throw broader exceptions than overridden methods

Example:

class A{

  public void m1(){}

}

classs B extends A{

  public void m1() throws Exception{}

  //Subclasses can never throw more exceptions than their parent

}

 

Interpretation: This causes compilation errors, exceptions occur in subclasses, and no exceptions are thrown by the parent

3. Arrays

1. Array A Reference Data Type

2. Arrays are a simple data type, linear structure

3. Arrays are containers that can be used to store other elements, and arrays can store elements of any data type.

4. Arrays are one-dimensional, two-dimensional, three-dimensional, and multi-dimensional.

5. The element types stored in the array are uniform.

6. The length of the array cannot be changed. Once the length of the array is created, it cannot become fixed.

7. The array uses the address of the first element of the array as the address to which the reference points.

8. Each element of an array is subscripted, indexed, and from 0 on, any array has a length attribute to get the number of elements in the array.Subscript array elements of the last element of the array minus 1.

9. Advantages of arrays: high search efficiency; obvious drawbacks: adding or deleting elements at will is inefficient.

For example:

 

package com.bjpowernode.java_learning;

​

public class D65_2_ArrayExcerse {

  public static void main(String[] args) {

    //Declare a one-dimensional array to store int type

    int[] a1 = {100,200,300,400};

    //boolean Type Array

    boolean a2 = {true,false,true};

    //String Type Array

    String[] a3 = {"zhansan","lisi","wangwu"};

    //Object array

    Object o1 = new Object();

    Object o2 = new Object();

    Object o3 = new Object();

    Object[] objs = {o1,o2,o3};

   

  }

}

 

4. Source code:

D65_1_ManualThrowException.java

D65_2_ArrayExcerse.java

https://github.com/ruigege66/Java/blob/master/D65_1_ManualThrowException.java

https://github.com/ruigege66/Java/blob/master/D65_2_ArrayExcerse.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Posted by sethi_kapil on Wed, 25 Dec 2019 11:03:50 -0800