如何在不终止程序的情况下抛出异常

时间:2017-04-20 02:18:33

标签: java exception

每次抛出异常,我的程序都会终止。有没有办法抛出异常而不终止它?我之所以想要这样做是出于测试目的。在我的最终产品中,我将简单地打印错误消息,但我有很多类和异常帮助我看到"错误"是。有时当我想知道条件是否被填满时,我会抛出异常,但不一定是程序破坏,这意味着程序可以在抛出异常后继续。

1 个答案:

答案 0 :(得分:3)

考虑使用try catch代替throws

try{
     //statements that may cause an exception
}
//Replace exception(type) with the exception you could be throwing
catch (exception(type) e)‏{ //Example for exception(type) is IOException
     //error handling code
     //e.printStackTrace();
}
相关问题