java抛出异常后继续执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9827143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
continuing execution after an exception is thrown in java
提问by R Doolabh
I'm trying to throw an exception (without using a try catch block) and my program finishes right after the exception is thrown. Is there a way that after I throw the exception, to then continue execution of my program? I throw the InvalidEmployeeTypeException which I've defined in another class but I'd like the program to continue after this is thrown.
我正在尝试抛出异常(不使用 try catch 块)并且我的程序在抛出异常后立即完成。有没有办法在我抛出异常之后继续执行我的程序?我抛出了在另一个类中定义的 InvalidEmployeeTypeException,但我希望程序在抛出后继续。
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);
}
}
采纳答案by rs.
Try this:
尝试这个:
try
{
throw new InvalidEmployeeTypeException();
input.nextLine();
}
catch(InvalidEmployeeTypeException ex)
{
//do error handling
}
continue;
回答by manub
If you throw the exception, the method execution will stop and the exception is thrown to the caller method. throw
always interrupt the execution flow of the current method. a try
/catch
block is something you could write when you call a method that may throw an exception, but throwing an exception just means that method execution is terminated due to an abnormal condition, and the exception notifies the caller method of that condition.
如果抛出异常,则方法执行将停止并将异常抛出给调用方方法。throw
总是中断当前方法的执行流程。一try
/catch
块的东西,当你调用可能会抛出异常的方法,你可以写,但抛出一个异常,只是意味着方法执行因终止异常情况,异常通知条件的主叫方法。
Find this tutorial about exception and how they work - http://docs.oracle.com/javase/tutorial/essential/exceptions/
查找有关异常及其工作原理的教程 - http://docs.oracle.com/javase/tutorial/essential/exceptions/
回答by j_v_wow_d
If you have a method that you want to throw an error but you want to do some cleanup in your method beforehand you can put the code that will throw the exception inside a try block, then put the cleanup in the catch block, then throw the error.
如果你有一个方法想要抛出错误,但你想事先在你的方法中做一些清理,你可以将抛出异常的代码放在 try 块中,然后将清理放在 catch 块中,然后抛出错误。
try {
//Dangerous code: could throw an error
} catch (Exception e) {
//Cleanup: make sure that this methods variables and such are in the desired state
throw e;
}
This way the try/catch block is not actually handling the error but it gives you time to do stuff before the method terminates and still ensures that the error is passed on to the caller.
这样 try/catch 块实际上并没有处理错误,但它让您有时间在方法终止之前做一些事情,并且仍然确保将错误传递给调用者。
An example of this would be if a variable changed in the method then that variable was the cause of an error. It may be desirable to revert the variable.
这方面的一个例子是,如果方法中的一个变量发生了变化,那么该变量就是导致错误的原因。可能需要恢复变量。