-5

I don't fully understand what the throws keyword in Java is for.

In particular, I can't seem to find clear information about what happens if I were to throw an exception type T from a Java function which declares that only exceptions of type Y can be thrown.

For example, something like this:

void testFunction () throws SomeExceptionType {
    throw new SomeDifferentExceptionType();
}

In this case I have attempted to illustrate that the expected exception type does not match the exception type which is actually thrown. Is this syntactically legal Java? If so, what happens at runtime?


I come from a C++ background, and in C++ we have the concept of noexcept. This keyword is used to mark functions as noexcept which means that they are not permitted to throw an exception. If a function marked noexcept does throw an exception, then std::terminate is immediately called, which borks program execution.

std::terminate is effectively like a "goto" statement in that it takes you to the end of program execution, quitting the program runtime. It may also do some stack clean-up for RAII, this is actually implementation defined, apparently.

So, is Java's throws statement equivalent to C++ noexcept, but inverted?


Although there are lots of questions here which ask how to use throws, I haven't been able to find a clear answer to this specific question about what happens if you throw something different to the exception type specified with throws.


Edit: I understand that this is only really relevant to the concept of checked exception types, because unchecked exception types "do not interact with" the throws keyword. Knowing this doesn't however provide an answer to the question being asked.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 2
    I don't know enough about Java to confirm but do note that in C++ the exception specification before C++11 used the same syntax, and might be why Java uses it. You can read about how the old C++ way worked here: https://en.cppreference.com/w/cpp/language/except_spec – NathanOliver Mar 21 '23 at 15:43
  • 2
    I suppose you want first to learn about checked/unchecked https://www.baeldung.com/java-checked-unchecked-exceptions – Iłya Bursov Mar 21 '23 at 15:44
  • 1
    @teapot418 isn't the first part only the case for checked Exceptions? – user16320675 Mar 21 '23 at 15:52
  • @IłyaBursov Yes, knowing the difference between both doesn't get any closer to answering the question. I knew about both before asking this. – FreelanceConsultant Mar 21 '23 at 16:19
  • 2
    @FreelanceConsultant you just cannot compile code which tries to throw checked exception not mentioned in throws, and answer posted also tells the same `If not, you will get a compilation error.` – Iłya Bursov Mar 21 '23 at 16:28
  • @IłyaBursov Ok that is the answer I was looking for. I didn't understand that part of the answer. Thanks – FreelanceConsultant Mar 21 '23 at 16:53
  • Duplicate of [Checked vs Unchecked exception](https://stackoverflow.com/questions/4639432/checked-vs-unchecked-exception) – Mark Rotteveel Mar 23 '23 at 06:53
  • @MarkRotteveel No it isn't – FreelanceConsultant Mar 23 '23 at 09:35
  • I think it does, because it explains what checked exceptions are, why you must use `throws` with checked exceptions, and that you get a compilation error otherwise if a checked exception can be thrown by code. – Mark Rotteveel Mar 23 '23 at 09:43
  • @MarkRotteveel I knew the difference between checked and unchecked exceptions prior to asking this question, so that would make it somewhat pointless to have asked the question if it was indeed a duplicate. – FreelanceConsultant Mar 23 '23 at 09:48
  • @MarkRotteveel I did not ask "what is an unchecked or checked exception" I asked what happens if you try to throw one or the other from a function which doesn't declare them with `throws`. It's a totally different question, even if they are about the same subject matter. – FreelanceConsultant Mar 23 '23 at 09:49
  • From the [topvoted answer](https://stackoverflow.com/a/4639560/466862): _"Compiler will never force you to catch such exception or force you to declare it in the method using `throws` keyword."_. – Mark Rotteveel Mar 23 '23 at 09:51
  • @MarkRotteveel Of what relevance is that – FreelanceConsultant Mar 24 '23 at 19:00
  • You say you asked _"[..] what happens if you try to throw one or the other from a function which doesn't declare them with throws."_ And the answer is that for unchecked exceptions the compiler doesn't force you to catch or declare `throws`, and for checked exception (for which I neglected to include a relevant quote from the duplicate in my previous comment), the compiler will force you to add `throws` or catch them. In other words, the duplicate does answer your question. – Mark Rotteveel Mar 25 '23 at 09:35
  • @MarkRotteveel Again, of what relevance is that? Two questions may be different but have similar, or even the same, answer – FreelanceConsultant Mar 25 '23 at 18:41

1 Answers1

3

In Java there are two types of exceptions - checked and unchecked. Checked exceptions are all the subclasses of Exception except those which are subclasses of RuntimeException.

Which means exceptions which are descendants of RuntimeException are unchecked exceptions.

The throws clause declares which checked exceptions the method is allowed to throw. It can throw any unchecked exception, but only checked exceptions which are declared in its throws clause (or their subclasses).

It has nothing to do with noexcept.

Syntactically what you wrote is not correct, because you are supposed to write throw new SomeDifferentExceptionType() - you can't throw a class. But assuming that's what you meant, if SomeDifferentExceptionType is unchecked, it will also work well. If not, you will get a compilation error.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Yes that's what I meant. It was intended to be pseduocode, but I will edit it to make it clearer. I didn't know the exact syntax. – FreelanceConsultant Mar 21 '23 at 16:10
  • Btw this doesn't answer the question - at all. Is it possible to throw a checked exception which isn't declared with `throws`, or not? Is this a runtime error? A compile time error? – FreelanceConsultant Mar 21 '23 at 16:18
  • @FreelanceConsultant what keeps you from trying yourself? Just write an empty method where all you do is `throw new IOException();`, for example. Then you'll see that this is a compiler error. – QBrute Mar 21 '23 at 16:34
  • @QBrute I don't have a JVM at work! Otherwise yes I would try it, but that might not necessarily give a clear understanding. The alternative might be to buy a textbook - but I also don't have one of those to hand (or even own one) – FreelanceConsultant Mar 21 '23 at 16:52