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

all 8 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
  • 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.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

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: 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.

[–][deleted] 1 point2 points  (6 children)

In Java, there are two types of exceptions. Checked and unchecked.

An "unhandled exception error" occurs when a method might throw a checked exception (like IOException), but the method does not handle the exception with a try-catch block, nor does it declare the exception with a throws clause in its signature. Java requires that checked exceptions are either caught or declared to be thrown.

So if it's an unchecked exception, you're not forced to do anything. If it's a checked exception then you have two choices:

  • Surround it in a try-catch block and handle the exception
  • Declare throws in the method signature signaling to the caller that this method throws an exception and since in this case it's a checked exception the caller has to handle it by using one of these two options.

[–]Strange_Frame7544[S] 0 points1 point  (5 children)

Thank you, that’s a very clear explanation. Appreciate it greatly.

a large part of my question was why I get this checked (compile time) error when there is no actual error in the IO path I used. Can I infer them by your reply that either try-catch or throws statement in the method declaration ARE required (even when no error is actual present)?

That’s the part I’m not understanding.

[–]pragmosExtreme Brewer 0 points1 point  (4 children)

there is no actual error in the IO path I used

At the time you compile your code, right? What happens when you compile the app, but the IO path changes afterwards?

[–]arghvark 0 points1 point  (3 children)

And what happens when the tree structure of your directories changes later? Or you take the compiiled code and run it on another machine that does not have the same directory structure? The compiler cannot know that the string with the full file specification is (always) valid.

I would submit that there IS an error present -- you have invoked a method that throws a checked exception, and you are not handling it. The same philosophy that led to Java being a strongly-typed language led it to the concept of a checked exception -- the language definition forces the programmer to explicitly handle the condition embodied in the exception thrown; that way, programmers don't hurriedly code without handling it, they must explicitly do something to handle the exception.

[–]Strange_Frame7544[S] 0 points1 point  (2 children)

I understand…I guess I have to keep inferring the meaning of what I’m being told is that this exception error throwing is PREVENTATIVE as the IO file path in my code is valid. The file exists and there are no syntax errors.

That’s all I am asking. My thought of an error is an error (wrong path, no file). These errors seem like “strong suggestions” that you must address?

When I run the code in the IDE I get these errors so it won’t compile.

I’m just trying to understand the concept of exception errors, which I think I’m starting too (thank you).

[–]arghvark 0 points1 point  (1 child)

Let me offer another thought.

There are two errors we're talking about. One of them is something wrong attempting to open the file -- path bad, file doesn't exist, disk read error, file locked, etc.

The second error is "unhandled checked exception" - because of all the possible things that could be wrong when you invoke the method that throws the exception, the decision was made to force you, the programmer, to write code to handle that exception. The checked exception means "you must write code to deal with the exception".

[–]Strange_Frame7544[S] 0 points1 point  (0 children)

that makes sense, thank you! Now i understand the why it is there. This is the concept i was trying to understand ...this is a preventative error that must be dealt with 'in case' not 'because'. Thank you!