#Return statement just after a catch bloc ?

1 messages · Page 1 of 1 (latest)

red light
#

What would happen in the following method in case of try..catched exception, that will throw an exception ? Notice the return 0; is it unreachable ?

public int calculateAmount(Data data, int coefficient){

  try {
    int price = 0;
    // do something 
    price *= executeCalculation(data.getBase(), coefficient);
    return price;
  } catch (Exception ex) {
    throw new IllegalArgumentException("some params aren't valid");
  }

  return 0; // is this unreacheable in case of exception ?
}
frank daggerBOT
#

<@&987246883653156906> please have a look, thanks.

indigo swan
#

yeah seems like it is unreachable to me

#

I think you can even remove it iirc

#

java should be able to tell that it is unreachable

red light
#

ah i know why

#

thanks

frank daggerBOT
#

Closed the thread.

indigo swan
#

I tested it

#

and it gave me an error

#

based on specs

red light
#

oh it gaves error

#

i thought it'd do warn only

indigo swan
#

also dont catch Exception please

red light
indigo swan
#

Exception literally catches everything, instead catch the specific exception

#

especially because you afterwards throw IllegalArgumentException though you cant be sure the exception comes from that

red light
#

there are some method calls that have their own try..catch

#

and generally, the possible exceptions are about args

indigo swan
#

also I personally would use proper validation using if + throw illegalargumentexception instead

red light
#
if(ex instanceof SomeException someException){
 error = "something went wrong while trying to ";
}
frank daggerBOT
red light
#

and a general message if none are matched

indigo swan
#

you really doing instanceof checks on exceptions?

#

that looks like a bad design

red light
indigo swan
#

because try catch exists

#

to catch specific exceptions

red light
indigo swan
#

just because its possible doesnt mean its good

red light
indigo swan
#

thats pre optimizing

#

also I am not talking about speed

#

speed is completely irrelevant here

#

also that answer is from 2013

#

just so you know

red light
#

hm

indigo swan
#

for example this one

#

says something completely different

red light
#

alr , the dude is using instanceof

#

¯_(ツ)_/¯

indigo swan
#

not really

red light
indigo swan
#

read the first sentence

#

literally

#

lol

#

read

#

not just look at the code

red light
#

..unless you need to share some steps in common

#

i need that

indigo swan
#

ok, though I would disagree with that sentence in some cases

#

where a method makes more sense

#

but notice how its not catching Exception

#

but the specific exceptions

#

catch (NotAnInt | ParseError e)

red light
#

i know , that looks better maybe, but using instanceof does the same

indigo swan
#

and a finally block maybe makes sense as well

#

but again cant really tell without real context

#

if someone needs instanceof checks with exceptions there is usually a design issue in the first place

civic spade
civic spade
red light
#

the unchecked ones, will have a general message

#

alright, im gonna explain my point here

indigo swan
#

not every exception should have a general message though

#

some might be a serious bug

#

and you silent them

#

by creating a general message

red light
#

if i use multicatch blocs:

try {
  // do something that may fire exception(s)
} catch (ExceptionA ex) {
  log.erro("something went wrong while trying to do A")
  throw new illegalArgException(ex.message());
} catch (ExceptionB ex) {
  log.erro("something is wrong while trying to do B")
  throw new illegalArgException(ex.message());
}  catch (ExceptionC ex) {
  log.erro("something is wrong while trying to do C")
  throw new illegalArgException(ex.message());
}
frank daggerBOT
red light
#

a friendly msg would help to start investigating rather than technical and lengthy one

#

as for dev environment, yes, i'd print stacktrace

indigo swan
#

a generic message going to get ignored lol

red light
#

and a bug is issued

#

to sumup the design of an app is not only for dev purpose, it must care about other envs

red light
#

alright thanks for your help