如何在 java 中应用用户定义的 try catch 块
How to apply user defined try catch block in java
我是初学者,我已经完成了基本的 Nim 游戏。现在我想将 try catch
块应用到程序中以确保流控制是完美的。但是,我目前看到的资源是在main方法中捕获异常,所有创建的方法都必须在try
块中调用才能捕获异常。
在我的例子中,我创建了 class 的构造函数来调用 processCommands
面板。然后,此面板调用 class 中的其他方法。
我需要在面板中捕获用户输入 commands
以查看命令是否有效。
我试过:https://beginnersbook.com/2013/04/user-defined-exception-in-java/
但似乎我必须重新构造到目前为止构建的方法。有什么办法可以解决这个问题吗?
感谢任何帮助。感谢您的善意和耐心。
这是我的一部分 Nimsys
:
import java.util.ArrayList;
import java.util.Scanner;
class InvalidUserInput extends Exception {
public InvalidUserInput(String s) {
super(s);
}
}
public class Nimsys{
private NimModel nimModel;
public static void main(String[] args) throws InvalidUserInput {
Nimsys nimsys = new Nimsys();
nimsys.processCommands();
}
public Scanner in = new Scanner(System.in);
private void processCommands() {
this.nimModel = new NimModel();
System.out.println("Welcome to Nim\n");
while (true) {
System.out.print('$');
String [] commandin = in.nextLine().split(" ");
if (commandin[0].equalsIgnoreCase("addplayer")) {
addPlayer(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("removeplayer")) {
if (commandin.length > 1) {
removePlayer(commandin[1]);
} else {
removePlayer("");
}
}
if (commandin[0].equalsIgnoreCase("editplayer")) {
editPlayer(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("displayplayer")) {
if (commandin.length > 1 ){
displayPlayer(commandin[1]);
} else {
displayPlayer("");
}
}
if (commandin[0].equalsIgnoreCase("startgame")) {
startGame(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("resetstats")) {
if (commandin.length > 1 ){
resetStats(commandin[1]);
} else {
resetStats("");
}
}
if (commandin[0].equalsIgnoreCase("rankings")) {
if (commandin.length > 1 && commandin[1].equals("asc")) {
rankings(commandin[1]);
}
else if (commandin.length > 1 && commandin[1].equals("desc")) {
rankings(commandin[1]);
} else {
rankings("");
}
}
if (commandin[0].equalsIgnoreCase("exit")) {
if (commandin.length == 1) {
System.out.println("");
break;
}
}
}
}
void userCommandCheck(String [] command) throws InvalidUserInput {
if (command.length >2) {
throw new InvalidUserInput("Incorrect number of arguments supplied to command.");
}
String [] commandSet = {"addplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
for(String commandCell: commandSet) {
if (command[0] != commandCell) {
throw new InvalidUserInput("`"+command[0]+"'" + " is not a valid command.");
}
}
}
}
private void processCommands() {
this.nimModel = new NimModel();
System.out.println("Welcome to Nim\n");
while (true) {
System.out.print('$');
String [] commandin = in.nextLine().split(" ");
try {
switch (userCommandCheck(commandin[0])) {
case "addplayer":
addPlayer(commandin[1]);
default:
}
} catch (InvalidUserInput e) {
}
}
static String userCommandCheck(String command) throws InvalidUserInput {
String [] commandSet = {"addplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
for(String commandCell: commandSet) {
if (command.equalsIgnoreCase(commandCell)) {
return command.toLowerCase();
}
}
throw new InvalidUserInput("`"+command+"'" + " is not a valid command.");
}
}
最后,我就这样做了。我认为更清楚,因为我希望命令面板看起来干净。
class InvalidUserInput extends Exception {}
Main class {
private void processCommands() {
// commands inside
// try catch block
try {
String [] commandSet = {"addplayer", "addaiplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
boolean contains = Arrays.stream(commandSet).anyMatch(commandin[0]::equals); //check if commandin[0] in any of values in the commandSet
if (!contains) {
throw new InvalidUserInput();
}
} catch (Exception e) {
System.out.println("`"+commandin[0]+"`" + " is not a valid command.");
}
}
我是初学者,我已经完成了基本的 Nim 游戏。现在我想将 try catch
块应用到程序中以确保流控制是完美的。但是,我目前看到的资源是在main方法中捕获异常,所有创建的方法都必须在try
块中调用才能捕获异常。
在我的例子中,我创建了 class 的构造函数来调用 processCommands
面板。然后,此面板调用 class 中的其他方法。
我需要在面板中捕获用户输入 commands
以查看命令是否有效。
我试过:https://beginnersbook.com/2013/04/user-defined-exception-in-java/
但似乎我必须重新构造到目前为止构建的方法。有什么办法可以解决这个问题吗?
感谢任何帮助。感谢您的善意和耐心。
这是我的一部分 Nimsys
:
import java.util.ArrayList;
import java.util.Scanner;
class InvalidUserInput extends Exception {
public InvalidUserInput(String s) {
super(s);
}
}
public class Nimsys{
private NimModel nimModel;
public static void main(String[] args) throws InvalidUserInput {
Nimsys nimsys = new Nimsys();
nimsys.processCommands();
}
public Scanner in = new Scanner(System.in);
private void processCommands() {
this.nimModel = new NimModel();
System.out.println("Welcome to Nim\n");
while (true) {
System.out.print('$');
String [] commandin = in.nextLine().split(" ");
if (commandin[0].equalsIgnoreCase("addplayer")) {
addPlayer(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("removeplayer")) {
if (commandin.length > 1) {
removePlayer(commandin[1]);
} else {
removePlayer("");
}
}
if (commandin[0].equalsIgnoreCase("editplayer")) {
editPlayer(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("displayplayer")) {
if (commandin.length > 1 ){
displayPlayer(commandin[1]);
} else {
displayPlayer("");
}
}
if (commandin[0].equalsIgnoreCase("startgame")) {
startGame(commandin[1]);
}
if (commandin[0].equalsIgnoreCase("resetstats")) {
if (commandin.length > 1 ){
resetStats(commandin[1]);
} else {
resetStats("");
}
}
if (commandin[0].equalsIgnoreCase("rankings")) {
if (commandin.length > 1 && commandin[1].equals("asc")) {
rankings(commandin[1]);
}
else if (commandin.length > 1 && commandin[1].equals("desc")) {
rankings(commandin[1]);
} else {
rankings("");
}
}
if (commandin[0].equalsIgnoreCase("exit")) {
if (commandin.length == 1) {
System.out.println("");
break;
}
}
}
}
void userCommandCheck(String [] command) throws InvalidUserInput {
if (command.length >2) {
throw new InvalidUserInput("Incorrect number of arguments supplied to command.");
}
String [] commandSet = {"addplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
for(String commandCell: commandSet) {
if (command[0] != commandCell) {
throw new InvalidUserInput("`"+command[0]+"'" + " is not a valid command.");
}
}
}
}
private void processCommands() {
this.nimModel = new NimModel();
System.out.println("Welcome to Nim\n");
while (true) {
System.out.print('$');
String [] commandin = in.nextLine().split(" ");
try {
switch (userCommandCheck(commandin[0])) {
case "addplayer":
addPlayer(commandin[1]);
default:
}
} catch (InvalidUserInput e) {
}
}
static String userCommandCheck(String command) throws InvalidUserInput {
String [] commandSet = {"addplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
for(String commandCell: commandSet) {
if (command.equalsIgnoreCase(commandCell)) {
return command.toLowerCase();
}
}
throw new InvalidUserInput("`"+command+"'" + " is not a valid command.");
}
}
最后,我就这样做了。我认为更清楚,因为我希望命令面板看起来干净。
class InvalidUserInput extends Exception {}
Main class {
private void processCommands() {
// commands inside
// try catch block
try {
String [] commandSet = {"addplayer", "addaiplayer", "removeplayer", "editplayer", "displayplayer", "startgame", "resetstats", "rankings"};
boolean contains = Arrays.stream(commandSet).anyMatch(commandin[0]::equals); //check if commandin[0] in any of values in the commandSet
if (!contains) {
throw new InvalidUserInput();
}
} catch (Exception e) {
System.out.println("`"+commandin[0]+"`" + " is not a valid command.");
}
}