every time I throw an exception, my program terminates. Is there a way to throw an exception without terminating it? The reason why I'd want to do it is for testing purposes. In my final product, I will simply print error messages, but I have a lot of classes and exceptions help me see where "errors" are. I throw exceptions sometimes when I want to know if a condition was filled, but isn't necessarily program breaking, meaning that the program could go on after throwing the exception.
Asked
Active
Viewed 3,571 times
-1
-
Why do you want to do this with an exception? – user2357112 Apr 20 '17 at 02:20
-
No, You can't throw exception and your program still running. – Fady Saad Apr 20 '17 at 02:30
-
I said in the program description that I like exceptions because they give the line number of the error – Meepo Apr 20 '17 at 02:44
-
And what user77... claims is total nonsense. – GhostCat Apr 20 '17 at 02:57
-
1[You can print a stack trace without throwing an exception.](http://stackoverflow.com/questions/944991/is-there-a-way-to-dump-a-stack-trace-without-throwing-an-exception-in-java) – user2357112 Apr 20 '17 at 07:18
-
Oh interesting user23, that's really helpful! – Meepo Apr 21 '17 at 01:43
1 Answers
3
Consider using try catch
instead of 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();
}

Cardinal System
- 2,749
- 3
- 21
- 42