Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I throw exceptions in an if-else block?

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?

like image 476
Lauda Wang Avatar asked Dec 27 '18 07:12

Lauda Wang


People also ask

Can you throw an exception in an if statement?

should I use the throw outside or inside an "if" statement? You definitely should throw exception inside the if condition check.

Is it good practice to throw exception in try block?

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.

How do you handle exceptions in if else in Java?

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.

Should you always throw an exception?

In short: You should throw an exception if a method is not able to do the task it is supposed to do.

What is an except block?

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 to put an exception in a try block?

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.

What happens when you throw an exception in Python?

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.

When to use if/else in exception handling?

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.


1 Answers

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; } 
like image 70
Eran Avatar answered Oct 04 '22 02:10

Eran