0

I have code something like this pseudo code:

void doSomething(int data) throws Exception{
    if(data < 10) throw new Exception("Exception thrown"); // throws an exception based on a condition
    else { ... } // logic goes here
}

When I try to call doSomething in the main function, it give me an error:

public static void main(){
    doSomething(11); // Error! unreported exception 
}

Here is a link to the previous example.

So I have to do one of the following:

  1. Add a try catch block around every doSomething call
  2. Add a throws statement in main
  3. Get rid of the throws statement in doSomething
  4. Make the condition a precondition, so that not following it results in undefined behavior or something similar.

3 will not work, because doSomething may throw an exception when a client is using it. 1 and 2 are simply redundant, and I think they should be avoided.

Finally, 4 is the most appealing to me (being primarily a C++ coder) but runs contrary to Java programming.

My question is: What is the best way out of the mentioned options (or any other options), and what is the best way to implement it?

Captain Hatteras
  • 490
  • 3
  • 11
  • 3
    I'm not sure I understand the issue. A checked exception will be documented; anything that uses a function that throws a checked exception will either try/catch it, or declare that it's re-thrown--that's just how checked exceptions work. Runtime exceptions do not have this restriction. There's nothing "contrary" about #4 other than it means your function could be called with invalid arguments--that's the same in all languages, whether or not it makes *sense* is context-dependent. – Dave Newton Oct 01 '21 at 18:11
  • I am relatively new to java, so sorry if I am making an issue out of nothing. Let me rephrase. I am making a function call with arguments that I know will never throw an exception. Do I still have to use a try catch block or a throws declaration when I am making this function call, or can I avoid using redundant code for a specific function call? – Captain Hatteras Oct 01 '21 at 18:23
  • Why not use the unchecked `IllegalArgumentException`? – shmosel Oct 01 '21 at 18:24
  • 1
    Option 2 is the best choice. Any time you aren’t sure what do to with an exception, propagate it, and make the calling code decide what should be done. – VGR Oct 01 '21 at 19:09

1 Answers1

1

It really depends on the context You are working with.

If You want the code to stop executing at the moment the Exception is thrown, You could use Runtime exceptions, You don't have to catch them.

The good use case would be a REST endpoint. If something goes wrong during the computation of the response, we could throw ResponseStatusException - the Runtime exception which would immedeately return the Http Response Error to the client and stop any further execution of the code.

In contrary, if You have a logic which has to be executed even if the exception was thrown, the best way would be to use try - catch blocks, or to add throws statement to the method declaration and try - catch in parent method.

Pavlo Stepura
  • 363
  • 2
  • 8