예외를 throw하려는 중입니다 (try catch 블록을 사용하지 않고) 예외가 발생하면 바로 프로그램이 완료됩니다. 예외를 던진 다음 프로그램을 계속 실행하는 방법이 있습니까? 나는 다른 클래스에서 정의한 InvalidEmployeeTypeException을 던집니다. 그러나이 예외가 발생하면 프로그램을 계속 진행하고 싶습니다.자바에서 예외가 throw 된 후 계속 실행
private void getData() throws InvalidEmployeeTypeException{
System.out.println("Enter filename: ");
Scanner prompt = new Scanner(System.in);
inp = prompt.nextLine();
File inFile = new File(inp);
try {
input = new Scanner(inFile);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
System.exit(1);
}
String type, name;
int year, salary, hours;
double wage;
Employee e = null;
while(input.hasNext()) {
try{
type = input.next();
name = input.next();
year = input.nextInt();
if (type.equalsIgnoreCase("manager") || type.equalsIgnoreCase("staff")) {
salary = input.nextInt();
if (type.equalsIgnoreCase("manager")) {
e = new Manager(name, year, salary);
}
else {
e = new Staff(name, year, salary);
}
}
else if (type.equalsIgnoreCase("fulltime") || type.equalsIgnoreCase("parttime")) {
hours = input.nextInt();
wage = input.nextDouble();
if (type.equalsIgnoreCase("fulltime")) {
e = new FullTime(name, year, hours, wage);
}
else {
e = new PartTime(name, year, hours, wage);
}
}
else {
throw new InvalidEmployeeTypeException();
input.nextLine();
continue;
}
} catch(InputMismatchException ex)
{
System.out.println("** Error: Invalid input **");
input.nextLine();
continue;
}
//catch(InvalidEmployeeTypeException ex)
//{
//}
employees.add(e);
}
}
이것이 예외 사용 방법의 좋은 예라고 생각하지 않습니까? – manub
이것은 완벽하게 작동했습니다. 나는 오류 처리를 처리하고 실행을 계속할 수 있었다. –
왜이 코드가 정중하게 표현 된 오프닝 코멘트에서 '좋은 예가 아닌가'를 평가하는 데 시간이 걸렸다. 'input.nextLine();'은 절대로 실행되지 않습니다. – Reg