Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw an exception in try catch block

try {    if (isFileDownloaded)    // do stuff   else    throw new CustomException() }  catch (Exception e) {   // something went wrong to save the error to log } finally {   //release resources } 

My question is would the catch catches the ApplicationException thrown in the try block? is it in poor coding style?

Should it be written in another way?

like image 811
Quincy Avatar asked Jul 20 '10 19:07

Quincy


People also ask

How do you throw an exception in try-catch block?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.

Can we use Throw in try-catch block?

throw: The throw keyword is used to transfer control from the try block to the catch block. 4. throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.

Can we throw exception from TRY block in Java?

Java try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.

What happens when an exception is thrown in a try block?

Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.


2 Answers

The catch will catch your exception (and any other that occurs). That being said, I try to avoid writing code like this when possible.

Personally, I see little reason to ever have exception handling (catch) for an exception thrown in the same scope. If you can handle your error in your method - put the exception handling (ie: logging) directly in the try block as well.

Using a catch is more useful, IMO, for catching exceptions thrown by methods within your try block. This would be more useful, for example, if your // do stuff section happened to call a method that raised an exception.

Also, I recommend not catching every exception (Exception e), but rather the specific types of exceptions you can handle correctly. The one exception to this would be if you're rethrowing the exception within your catch - ie: using it for logging purposes but still letting it bubble up the call stack.

like image 139
Reed Copsey Avatar answered Sep 23 '22 12:09

Reed Copsey


Yes, it will catch ApplicationException as it derives from Exception.

Handling the base exception should be fine in most cases unless you need to log or do something with a different type of exception...

try {     if (isFileDownloaded)        doSomeThings();     else        throw new ApplicationException("Something expectedly unexpected happened."); } catch(ApplicationException e) {    // log application exception here... } catch(Exception e) {    // log all other exceptions here... } finally {    // release resources... } 
like image 34
Alex Avatar answered Sep 23 '22 12:09

Alex