gpt4 book ai didi

java - 异常 try catch 将名称拆分为名字和姓氏

转载 作者:行者123 更新时间:2023-11-29 05:41:24 24 4
gpt4 key购买 nike

我被要求编写一段程序来使用 try 和 catch 处理异常。虽然,当我运行它时,它不会反射(reflect)我的异常代码,因为应该打印“无效名称”。任何人都可以指出原因吗?如果有人更正我的代码,也将受到热烈欢迎!谢谢。

我需要在以下条件下编写一个程序:

导入 javax.naming.InvalidNameException。写一个方法 public void printName(String name) throws InvalidNameException如果名称没有空格,该方法应抛出 InvalidNameException。写一个驱动来测试。

===============Code result=============
Enter your name: James Dean
First name: James
Last name: Dean
Enter your name: Brian Smith
First name: Brian
Last name: Smith
Enter your name: RuthKnight
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Name.printName(Name.java:33)
at NameApp.main(NameApp.java:17)
==================Method============================
import javax.naming.InvalidNameException;

public class Name{

public void printName(String name) throws InvalidNameException{

String [] nameSplit = name.split(" ");
String first = nameSplit[0];
String last = nameSplit[1];

System.out.println("First name: "+first);
System.out.println("Last name: "+last);
}

}
======================Driver========================
import java.util.Scanner;
import javax.naming.InvalidNameException;


public class NameApp{

public static void main(String[] args) {

Name aa = new Name();
Scanner console = new Scanner(System.in);
try {
boolean keepRunning= true;

while(keepRunning){
System.out.print("Enter your name: ");
String in = console.nextLine();
aa.printName(in);
}
}
catch(InvalidNameException e){
System.out.println("invalid name");
}
}

}

最佳答案

我建议在 Name 类中捕获异常,然后像这样抛出 InvalidNameException:

public void printName(String name) throws InvalidNameException {
try {
String[] nameSplit = name.split(" ");
String first = nameSplit[0];
String last = nameSplit[1];

System.out.println("First name: " + first);
System.out.println("Last name: " + last);
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidNameException("Missing space in: " + name);
}
}

更新

运行这段代码:

import javax.naming.InvalidNameException;
public class NameApp{
public static void main(String[] args) {
Name aa = new Name();
try {
System.out.print("Enter your name: ");
String in = "George Den";
aa.printName(in);
in = "George";
aa.printName(in);
}
catch(InvalidNameException e){
System.out.println(e.toString());
}
}
}

import javax.naming.InvalidNameException;
public class Name {
public void printName(String name) throws InvalidNameException {
try {
String[] nameSplit = name.split(" ");
String first = nameSplit[0];
String last = nameSplit[1];
System.out.println("First name: " + first);
System.out.println("Last name: " + last);
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidNameException("Missing space in: " + name);
}
}
}

将打印这个:

Enter your name: First name: George Last name: Den

javax.naming.InvalidNameException: Missing space in: George

更新 2

考虑@robjohncox 的建议。此代码不应按原样使用,因为 javax.naming.InvalidNameException 应仅在特定情况下抛出,例如 LDAP 查找和一般的目录特定操作。

最适合您的是对异常对象进行子类化并创建您自己的异常类型(另请参见 How can I write custom Exceptions?):

public class MyCustomNamingException extends Exception {
public MyCustomNamingException(){
}
public MyCustomNamingException(String message){
super(message);
}
}

然后在您的代码中使用此 MyCustomNamingException,如之前使用 InvalidNameException

所示

关于java - 异常 try catch 将名称拆分为名字和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com