Here is the code:
public Response getABC(Request request) throws Exception { Response res = new Response(); try { if (request.someProperty == 1) { // business logic } else { throw new Exception("xxxx"); } } catch (Exception e) { res.setMessage(e.getMessage); // I think this is weird } return res; }
This program is working fine. I think it should be redesigned, but how?
should I use the throw outside or inside an "if" statement? You definitely should throw exception inside the if condition check.
An exception is a blocking error. First of all, the best practice should be don't throw exceptions for any kind of error, unless it's a blocking error. If the error is blocking, then throw the exception.
You could do this by looking into the Javadoc or use your favorite IDE. If you catch Exception as the Exception class, it catches every Exception that is subclass of it. To achieve different Exceptions thrown in your code the methods should at least throw different exceptions. In your example with file.
In short: You should throw an exception if a method is not able to do the task it is supposed to do.
What follows is an except block. When you don’t specify which exception to catch, it will catch any. In other words, this is generic for exceptions.
When you think a part of your code might throw an exception, put it in a try block. Let us see a Python try exception example. What follows is an except block. When you don’t specify which exception to catch, it will catch any.
In other words, this is generic for exceptions. When an exception is thrown in a try block, the interpreter looks for the except block following it. It will not execute the rest of the code in the try block. Python Exceptions are particularly useful when your code takes user input.
Take in account that in case of if/else you're going to perform that check every time, regardless if execution was successful or not. Thus if error case is occurring rarely or practically never under normal circumstances, then exception handling is way more efficient, as on successful execution you're not evaluating any additional conditions.
It makes no sense to throw an exception in a try block and immediately catch it, unless the catch block throws a different exception.
Your code would make more sense this way:
public Response getABC(Request request) { Response res = new Response(); if (request.someProperty == 1) { // business logic } else { res.setMessage("xxxx"); } return res; }
You only need the try-catch block if your business logic (executed when the condition is true
) may throw exceptions.
If you don't catch the exception (which means the caller will have to handle it), you can do without the else
clause:
public Response getABC(Request request) throws Exception { if (request.someProperty != 1) { throw new Exception("xxxx"); } Response res = new Response(); // business logic return res; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With