Java 解析异常到自定义异常
Java Parse Exception to Custom Exception
我是 Java 的新手,我的问题是简单年龄计算器。这是我的代码:
public class Client {
public int findAge(String birthDate) throws InvalidDateFormatException {
// InvalidDateFormatException is a custom defined
int age = 0;
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (age >= 0) ? age : 0;
}
主要是
try {
System.out.println(c.findAge("08-09-1015"));
} catch (InvalidDateFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在,每次我以错误的格式传递字符串时都会抛出 ParseException。有什么方法可以让它抛出 InvalidDateFormatException 异常吗?
此外,如果我遵循正确的编码标准并遵守最佳实践,请对我的代码的风格和质量发表评论。
要回答您的主要问题,您需要在 catch
块中抛出自定义异常:
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
throw new InvalidDateFormatException("invalid date: " + birthDate);
}
关于您的代码,我有几点建议:
- 不要使用
new GregorianCalendar()
而更喜欢 Calendar.getInstance()
.
- 你计算年龄的方式有问题:你没有考虑月份和日期(假设我们是 2015-09-20,出生日期是 2014-12-01,你的代码即使宝宝还不到1岁也会输出1。
- 考虑提供日期参数而不是字符串参数。
findAge
方法不应该负责解析出生日期。
- 如果您使用的是Java8,请考虑使用新的Java时间API。
为 InvalidDateFormatException 定义自定义异常 class,如下所示:
public class InvalidDateFormatException extends RuntimeException {
private String errorMessage;
public InvalidDateFormatException(String errorMessage, Exception exe) {
super(errorMessage);
exe.printStackTrace();
}
}
修改您的 catch 块以抛出异常,如下所示:
public class Client {
public int findAge(String birthDate) throws InvalidDateFormatException {
int age = 0;
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
throw new InvalidDateFormatException("Invalid Date Format while finding Age", e);
}
return (age >= 0) ? age : 0;
}
}
此外,我建议您浏览以下网站:
https://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html
我是 Java 的新手,我的问题是简单年龄计算器。这是我的代码:
public class Client {
public int findAge(String birthDate) throws InvalidDateFormatException {
// InvalidDateFormatException is a custom defined
int age = 0;
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (age >= 0) ? age : 0;
}
主要是
try {
System.out.println(c.findAge("08-09-1015"));
} catch (InvalidDateFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在,每次我以错误的格式传递字符串时都会抛出 ParseException。有什么方法可以让它抛出 InvalidDateFormatException 异常吗?
此外,如果我遵循正确的编码标准并遵守最佳实践,请对我的代码的风格和质量发表评论。
要回答您的主要问题,您需要在 catch
块中抛出自定义异常:
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
throw new InvalidDateFormatException("invalid date: " + birthDate);
}
关于您的代码,我有几点建议:
- 不要使用
new GregorianCalendar()
而更喜欢Calendar.getInstance()
. - 你计算年龄的方式有问题:你没有考虑月份和日期(假设我们是 2015-09-20,出生日期是 2014-12-01,你的代码即使宝宝还不到1岁也会输出1。
- 考虑提供日期参数而不是字符串参数。
findAge
方法不应该负责解析出生日期。 - 如果您使用的是Java8,请考虑使用新的Java时间API。
为 InvalidDateFormatException 定义自定义异常 class,如下所示:
public class InvalidDateFormatException extends RuntimeException {
private String errorMessage;
public InvalidDateFormatException(String errorMessage, Exception exe) {
super(errorMessage);
exe.printStackTrace();
}
}
修改您的 catch 块以抛出异常,如下所示:
public class Client {
public int findAge(String birthDate) throws InvalidDateFormatException {
int age = 0;
try {
Calendar past = new GregorianCalendar();
Calendar present = Calendar.getInstance();
past.setTime(new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(birthDate));
age = present.get(Calendar.YEAR) - past.get(Calendar.YEAR);
} catch (ParseException e) {
throw new InvalidDateFormatException("Invalid Date Format while finding Age", e);
}
return (age >= 0) ? age : 0;
}
}
此外,我建议您浏览以下网站: https://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html