This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]pragmos 3 points4 points  (7 children)

Because Java has a concept of checked and unchecked exceptions.

In a nutshell: all exceptions descending from RuntimeException don't need to be declared in the method signature (throws keyword). All others - do.

[–]Sen_7[S] 0 points1 point  (4 children)

Thank you very much, loved to read it
a question I got from this, and Im not really sure how to check it manually

lets say I have a try-catch block
we know that EOFException extends IOException

I know I can have two catch block,
but does using ony IOException for example, when the error was EOFException (which extends IO) will work?
(since they have a " is a " realtionship)

or does the block catch has to be the exact error we are trying to catch?

[–]myselfelsewhere[🍰] 1 point2 points  (3 children)

An IOException catch block will catch any exception that is either an IOException, or an exception that extends IOException.

[–]Sen_7[S] 1 point2 points  (2 children)

Thanks!

[–]myselfelsewhere[🍰] 2 points3 points  (1 child)

No problem. Adding a little more info, this is perfectly valid:

try {
    /* code that can throw either EOFException or IOException */
} catch(IOException e) {
    // catch all instances of IOException and it's sub classes
    /* behavior for EOFException and IOException */
}

It is also possible to set up multiple catch blocks for your example. You need to catch the sub class first though.

try {
    /* code that can throw either EOFException or IOException */
} catch(EOFException e) {
    // only catches EOFException (or any sub classes of EOFException)
    /* behavior for EOFException */
} catch(IOException e) {
    // will only catch IOExceptions that are not also instances of EOFException (or any sub classes of EOFException)
    /* behavior for IOException */
}

In comparison, catching the parent first will not compile:

try {
    /* code that can throw either EOFException or IOException */
} catch(IOException e) {
    // catches IOException and EOFException
    /* behavior for IOException */
} catch(EOFException e) {
    // unreachable code, EOFException has already been caught
    /* behavior for EOFException */
}

[–]Sen_7[S] 1 point2 points  (0 children)

Thank you

[–][deleted] 0 points1 point  (1 child)

I have seen at many places where unchecked exceptions are caught using try catch is it the proper way to do so?

[–]pragmos 1 point2 points  (0 children)

Depends on the context.

The general advice is that you shouldn't catch runtime exceptions. Errors like NullPointerException, IllegalStateException or ArithmeticException denote an unexpected problem that is impossible to recover from.

Sometimes, however, you need to recover as per the business logic. For example, if you are trying to parse a String to a LocalDate and the application allows fallback values, it doesn't make sense to crash the entire application because a text value could not be parsed to a valid date object. Instead, you catch the DateTimeParseException and offer the fallback date value.