r/javahelp • • Feb 01 '21

I think my teacher is wrong about checked and unchecked exceptions?

I’m not sure this is the right place to post this, but I feel like I’m going crazy so I’d appreciate any input you can give me. And just to qualify at the start here— I’m planning on just letting this one go. I can do it the way they want it for the course of the class. I don’t think I have anything to gain from making a big deal out of it.

Anyway, my understanding of checked exceptions is that they are the exceptions of the Exception superclass which are not subclasses of RuntimeException. They have the catch or specify requirement. As such, they are the exceptions we use try-catch blocks of code for. If I’m already wrong feel free to skip the rest and shame me in the comments.

That said, my teacher has been very firm that unchecked exceptions are the ones we use try-catch blocks for. I sent them an email with a link to the oracle Java tutorials that’d seemed to back up my understanding of things. At the very least, I was hoping for some guidance on why I might have misunderstood the page I linked or if there was a better place to get my supplemental information. The reply email I got just restated what I shared with you above.

Am I crazy??? I just need a little validation here. I am a novice so I’m not assuming I am more knowledgeable. However, I seem to have two conflicting sources here (the teacher and the internet) and it’s giving me a bit of anxiety. Any help you can give me would be appreciated.

16 Upvotes

19 comments sorted by

•

u/AutoModerator Feb 01 '21

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://imgur.com/a/fgoFFis) 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.

11

u/sozesghost Feb 01 '21

I don't have the full context of your conversation, but you CAN use try catch blocks for both checked and unchecked exceptions. You also HAVE to use the catch block for checked exceptions, or you need to declare that your method throws it, and let the caller worry about it (which I think you tried to say here They have the catch or specify requirement).

4

u/jesuscristourlard Feb 01 '21

I realize it would’ve helped for context but I was trying to keep it vague. In the context of our class, we are only interested in try-catch blocks in situations where you have to use them (we are learning input/output right now).

They said checked exceptions are the ones we leave for the compiler to handle and unchecked are the ones we handle with try-catch blocks.

12

u/Rulmeq Feb 01 '21

They said checked exceptions are the ones we leave for the compiler to handle and unchecked are the ones we handle with try-catch blocks.

They are wrong, but cut them some slack, everyone makes mistakes. Go grab the Oracle documentation and bring it with you if you feel the strong urge to correct them. But you have a good understanding from how you phrased it in your original post. As an aside, I haven't really seen "unchecked" exceptions called that, they are usually refered to as Runtime exceptions (which might make them less confusing). Also "checked" should give a very good hint that those are the ones you must expliclty handle.

5

u/jesuscristourlard Feb 01 '21

I appreciate your reply and advice! I already sent them a link so pushing it any further seems unnecessary. Really though, I was starting to doubt myself and it’s good to get some outside perspective.

4

u/_jetrun Feb 01 '21

They said checked exceptions are the ones we leave for the compiler to handle and unchecked are the ones we handle with try-catch blocks.

Heh. The compiler will handle the checked ones for you if you don't catch them. And by 'handle', it will complain and your compilation will fail.

At the end of the day, there's no point in debating this. Write a little test class and show it to them!

1

u/evils_twin Feb 02 '21

Still not quite enough context. Could it be they are saying that you don't have to catch an exception that is declared thrown by a method. So if a method is public void foo() throws SomeException, then you shouldn't have to catch SomeException within foo().

There's no such thing as an exception you leave for the compiler. If you know an exception might occur in your code whether checked or not, you should handle it.

1

u/jesuscristourlard Feb 02 '21

No, we haven’t gone over the “throws” keyword and the syllabus says we’re not using it at all. There seems to be some differences in this thread as to what exceptions constitute. What we are dealing with in class are what you refer to in your second paragraph— things such as FileNotFoundException and InputMismatchException, which we need to write try-catch blocks for — and the teacher refers to these as “unchecked exceptions” .

The ones we “leave for the compiler” are runtime exceptions such as NullPointerException and IndexOutOfBoundsException and the teacher refers to these as “checked exceptions”.

I’m gathering that the checked vs. unchecked language is a little specific and not everyone uses it, but as far as I can tell the teacher has their definitions of these two phrases backwards.

1

u/jesuscristourlard Feb 02 '21

I didn’t want to do a ninja edit on you but when I say “leave for the compiler” I meant the exceptions which are thrown by faulty code— so the ones that shouldn’t exist in a functioning, finished program. Like if you hard-coded a call to a negative index on an array. My examples of specific exceptions might have been bad— I’m learning this topic for the first time!

5

u/juliensaab Feb 01 '21

As you said, checked exceptions are exceptions that MUST be handled at compile time. Unchecked exceptions always extend RuntimeException, these kind of exceptions do not oblige you to surround it with a try catch, i.e: the compiler won't shout at you if you don't surround it with a try catch because it thinks that you are going to handle it at runtime. One example of RuntimeException is the NumberFormatException, which is thrown at runtime if the string cannot be parsed into a valid number.

3

u/juliensaab Feb 01 '21

And one thing to add, as an instructor, I like it when students point out that I made a mistake during the explanation of something (I used to do the same thing back when I was a student). Point the mistake out to your instructor and if in the next lecture they didn't mention their mistake in anyway, at the end of the session mention it again.

4

u/jesuscristourlard Feb 01 '21

Yes, this teacher has been receptive to student input in general, especially during class when we are ‘coding along’. However I already tried to email them about it (I included a source from oracle) and they are being fairly stubborn about their definition. If my grade was on the line I might be more upset but as this seems incidental I’m more inclined to let it go.

That said, I appreciate your advice! I definitely needed some outside perspective on the topic.

2

u/Orffyreus Feb 01 '21 edited Feb 01 '21

Yes, you're right theoretically. https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

RuntimeExceptions initially were designed to be used for programming errors (e. g. a programmer forgot to do a null check) and checked exceptions were designed to make the programmer aware, that no matter what he does, a recoverable exception can occur (e. g. a file gets deleted before you can read it).

In practice RuntimeExceptions often are used as recoverable exceptions and other languages (C#, Kotlin, ...) don't even bother to make a destinction between checked exceptions and RuntimeExceptions. Even the Java API itself has some questionable choices: Integer.parseInt can throw a NumberFormatException which is a RuntimeException and you could say, that's okay, because a programmer can check, if a string is a valid number and call Integer.parseInt only, if it is, but the Java API has no other builtin check than parsing a string with Integer.parseInt (and parsing is indeed the check one has to do, but without throwing a RuntimeException), so programmers use Integer.parseInt with try-catch blocks and recover from the NumberFormatException, an unchecked exception, by allowing a user to input the number again.

4

u/jesuscristourlard Feb 01 '21

Ah yes, thank you for the link! That was the page I was doing research on and I linked the page on “the catch or specify requirement” when I emailed them.

I also appreciate your insight on more specific circumstances where that distinction is complicated.

It seems like there is a lot of conflict on the subject of exceptions in general! I got a taste of that when doing research but that discussion is beyond my current understanding.

2

u/sombriks Feb 01 '21

Both checked and unchecked exceptions can be handled by try/catch.

The key diference is checked exceptions MUST be handled or the method throwing them must declare it in 'throws' section.

2

u/jesuscristourlard Feb 02 '21

I think I missed that there are situations where you can and should handle unchecked exceptions— so I appreciate that specification.

But yes, that was my understanding of what checked exceptions are, so thanks for that! It might seem obvious but my teacher was telling me the opposite definitions were true, so it was very confusing for me.

2

u/DrPeroxide Feb 02 '21

You are indeed correct; checked exceptions must be handled (compilation will fail if they are not) and unchecked exceptions can be handled, but don't need to be.

The difference between checked and unchecked exceptions is very simple and basic knowledge for junior devs. if your professor is getting this wrong, I can't imagine he's going to be right about much else.

Everyone here seems to be trying to give your professor the benefit of the doubt and that's a fair perspective. But in my opinion I would double check anything this professor teaches you from now on.

1

u/khooke Extreme Brewer Feb 01 '21

Without hearing the context of the discussion there may be some misunderstanding somewhere, but re. your comment "the ones we use try-catch blocks for" ...if you know your code could throw an exception, regardless of whether it's checked or unchecked, shouldn't you have a catch block to handle it somewhere? Unless for some reason you don't care and it's ok for the app to just end abnormally, in some cases that's a valid option.

1

u/TilionDC Feb 02 '21

Its like you said. I dont think anyone normally uses a try catch for nullpointers for example. Its just better to use optional or simply if not null.