package com.itheima.demo04.MyException;
import java.util.Scanner;
/*
Requirements: We simulated registration operation, if the user name already exists, an exception is thrown and tips: pro, that username has already been registered.
analysis:
1. Use the array holds already registered user name (database)
2. Use the Scanner captures user input registered user name (front page)
3. Define a method, the user name registered in the user input is determined
Traversing the storage array has been registered user name, access to each user name
Using the acquired user name and user name entered by the user compare
true:
User name already exists, an exception is thrown RegisterException inform users of "pro, the user name is already registered";
false:
Continue to traverse Compare
If the cycle is over, yet to find duplicate user name, user prompts, "Congratulations, the registration is successful!";
*/
public class Demo02RegisterException {
1. // array holding the already registered user name (database)
static String [] usernames = { "John Doe", "John Doe", "Wang Wu"};
public static void main(String[] args) {
// 2. Use Scanner captures user input registered user name (front page)
Scanner sc = new Scanner(System.in);
System.out.println ( "Please enter the user name you want to register:");
String username = sc.next();
checkUsername(username);
}
3 // Define a method, the user name registered in the user input is determined
public static void checkUsername(String username) {
// traverse the storage array has been registered user name, access to each user name
for (String name : usernames) {
// use the obtained user name and user name entered by the user compare
if(name.equals(username)){
// true: the user name already exists, an exception is thrown RegisterException inform users of "pro, the user name is already registered";
throw new RegisterException ( "pro, the user name is already registered"); // throws a runtime exception, without treatment, to the JVM processing, interrupt processing
}
}
// If the cycle is over, yet to find duplicate user name, user prompts, "Congratulations, the registration is successful!";
System.out.println ( "Congratulations, successfully registered!");
}
}
package com.itheima.demo04.MyException;
import java.util.Scanner;
/*
Requirements: We simulated registration operation, if the user name already exists, an exception is thrown and tips: pro, that username has already been registered.
analysis:
1. Use the array holds already registered user name (database)
2. Use the Scanner captures user input registered user name (front page)
3. Define a method, the user name registered in the user input is determined
Traversing the storage array has been registered user name, access to each user name
Using the acquired user name and user name entered by the user compare
true:
User name already exists, an exception is thrown RegisterException inform users of "pro, the user name is already registered";
false:
Continue to traverse Compare
If the cycle is over, yet to find duplicate user name, user prompts, "Congratulations, the registration is successful!";
*/
public class Demo01RegisterException {
1. // array holding the already registered user name (database)
static String [] usernames = { "John Doe", "John Doe", "Wang Wu"};
public static void main(String[] args) /*throws RegisterException*/ {
// 2. Use Scanner captures user input registered user name (front page)
Scanner sc = new Scanner(System.in);
System.out.println ( "Please enter the user name you want to register:");
String username = sc.next();
checkUsername(username);
}
3 // Define a method, the user name registered in the user input is determined
public static void checkUsername(String username) /*throws RegisterException*/ {
// traverse the storage array has been registered user name, access to each user name
for (String name : usernames) {
// use the obtained user name and user name entered by the user compare
if(name.equals(username)){
// true: the user name already exists, an exception is thrown RegisterException inform users of "pro, the user name is already registered";
try {
throw new RegisterException ( "pro, the user name is already registered");
} catch (RegisterException e) {
e.printStackTrace ();
return; // end method, a premature end to other content within the method is not performed
}
}
}
// If the cycle is over, yet to find duplicate user name, user prompts, "Congratulations, the registration is successful!";
System.out.println ( "Congratulations, successfully registered!");
}
}
Custom exception class:
package com.itheima.demo04.MyException;
/*
Custom exception class:
Java exception classes provided, enough for us to use, you need to define yourself some exception classes
format:
public class XXXExcepiton extends Exception | RuntimeException{
Adding a null constructor parameter
Adding a constructor with exception information
}
note:
1. custom exception class are generally end in Exception, such description is an exception class
2. The custom exception class must inherit Exception or RuntimeException
Inheritance Exception: So custom exception class is a compile-time anomaly, if the internal method throws an exception compile, you must handle the exception, or throws, or try ... catch
Inherited RuntimeException: So custom exception class is a runtime exception, without treatment, to the virtual machine processing (interrupt handling)
*/
public class RegisterException extends /*Exception*/ RuntimeException{
// add an empty constructor parameters
public RegisterException(){
super();
}
/*
Adding a constructor with exception information
View source found that all exception classes will have a constructor with exception information,
Internal method will call the parent class constructor with exception information,
Let the parent to handle this exception information
*/
public RegisterException(String message){
super(message);
}
}