So I have been doing a bit of looking but couldn't really find anything that answered my question. In my main method, I have a loop of strings that are going to be run into a method where after it will query 10-12 different tables based of off this string. I currently have the queries surrounded in try catch blocks to catch exceptions. What I want to happen is if there is a timeout or some other sql exception for the program to write to a log (which I have done already) and then return to the main method from any of the other methods it may be in, and go on to the next string in the loop. I don't know a lot about throwing exceptions that are caught farther up, so I though maybe that's what I need to do, but I couldn't find anywhere if I throw an exception if it runs up to where it is caught or what happens exactly with it. Do I just need to throw the exception in the query methods and surround the method call in the main method with a try catch block? Any help is greatly appreciated. Thanks
Asked
Active
Viewed 305 times
0
-
1I think you pretty much understand it ... if method A calls B which calls C which calls D which calls E etc. and E throws an exception, then if E doesn't catch it, it looks to see if the call in D is in a place where the exception will get caught. If not, then it looks in C, then B, then A. So if you have a `try..catch` block in `main`, it will catch your exception if it isn't caught somewhere further up the stack. – ajb Dec 04 '14 at 22:39
-
Thank you very much, I thought it was like that, but I wasn't really positive about it since I usually handle exceptions as they happen instead of throwing them to be caught higher up. – Tapialj Dec 04 '14 at 23:31
-
It's probably correct to handle some exceptions as they happen. Generally, I think the distinction is between "expected" exceptions that should be handled in a specific way; and exceptions that indicate that something has gone wrong enough that you have to abandon the whole operation, which is what I think you're looking for. If you want the "best" way to deal with exceptions, you'll find a lot of disagreement among experts. – ajb Dec 05 '14 at 17:27