2

I'm trying to improve my code and I found a place where you can find problems and you can upload the answer and it says if it works or if it doesn't.

The funny thing is that it asks for handling the exceptions but it says it's wrong if u handle them.

Comments
  • 0
    what website is it ?
  • 0
    How do you handle the exceptions ?

    generic catch all statements are pretty much always wrong.
  • 0
    @ItsNotMyFault common try/catch statement
  • 0
    @d02d33pak aceptaelreto. Spanish site.
  • 0
    @davidf98

    How do you write your catch statements.

    if you use C# for example then:
    catch { ... } is wrong.
    catch (Exception e) { ... } is also wrong.
  • 0
    @ItsNotMyFault (reply to myself since i can't edit posts in the web app)

    There are a few exceptions ofcourse (i.e, it can be ok to have a generic catch at the applications top level that catches anything that wasn't properly handled to be able to log the error before shutting down)
  • 0
    @ItsNotMyFault I'm coding in Java and I'm using try{}catch(Exception e){...

    The problem is that if I don't add the try/catch, the judge says RTE and if I add it, it says the output isn't correct.
  • 1
    @davidf98

    1) You should never catch the base exception class unless you want to perform some action before aborting the program in case of a serious error.

    normally you want a try block that calls a single method and then one catch statement for each exception type that method explicitly throws.

    i.e:
    try { opensomefile }
    catch (IOException e) { ... }
    catch (SecurityException e) { ... }
    finally { ... }
    Never catch the base Exception class unless you intend to give up and abort or atleast rethrow it. catching the base exception class at the top level of your application to log any unhandled errors before the application dies is acceptable, doing it elsewhere is a bad idea. (How do you handle an exception properly without knowing what type of exception it is ? and if you're not handling it properly then the catch statement is basically just swallowing and hiding bugs in your code)
  • 1
    Bad exception handling shouldn't affect the output of your application though, but it might hide the cause of the incorrect output.
  • 0
    @ItsNotMyFault Okey, thank you so much. I will keep looking for the error and I'll apply what u told me.
    (still a lot to learn I guess)
Add Comment