5

I came to Java from C++. Both in Java and C++ we have the possibility to specificate exceptions. It looks like this:

void function_name() throw(Exception)
{
  ...
  if (error) 
  {
    throw Exception("Error");
  }
  ...
}

As I know, writing exceptions specification is considered to be a bad practice in C++. Unlike C++, in Java we have to do this. So, my question is:

What's the benefit of writing exceptions specification in Java?

Brian
  • 14,610
  • 7
  • 35
  • 43
James Akwuh
  • 2,169
  • 1
  • 23
  • 25
  • 2
    AFAIK, worse than bad practice, it's even deprecated now (in C++) – Xaqq Jun 01 '15 at 19:51
  • I don't know about C++ exceptions but if by "specification" you mean writing a message...well...if my program blows up with an exception I'd love to have a message telling me what went wrong... – JSelser Jun 01 '15 at 19:54
  • 4
    No *exception specification* is a way to decorate the signature of a function with the type of exceptions that could be thrown inside... – Jean-Baptiste Yunès Jun 01 '15 at 19:56
  • @Jean-BaptisteYunès Ok, then why it's is considered to be a bad practice in C++ (it's even deprecated now), and at the same time we use *exception specification* in Java? What's the difference? – James Akwuh Jun 01 '15 at 19:59
  • 1
    IT is bad practice in C++, because they have been too lately standardized and differently integrated into compilers. Different compilers may produce different behaviors when specifying exceptions. See: https://msdn.microsoft.com/en-us/library/wfa0edys.aspx – Jean-Baptiste Yunès Jun 01 '15 at 20:04
  • [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) is very much related. – Mick Mnemonic Jun 01 '15 at 20:04
  • Not only exception specification is bad practice in C++, but exceptions itself somewhere too. See https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Exceptions for example. – Zefick Jun 01 '15 at 20:09
  • You cannot compare java and c++ exception specifications it's a completely different concept. Java exception specifications are checked during compile time, in c++ they are checked during runtime. – tomse Jun 01 '15 at 20:20
  • 1
    @Zefick Because google doesn't use exception in its legacy code base it doesn't mean they are bad. Exception are a good thing that leverage RAII pretty well. – Xaqq Jun 01 '15 at 20:20
  • Exception specifications besides `std::throw()` or `noexcept` are impossible to track at compile time. The result is a compiler generated overhead, which the programmer should account for (In other words, these are useless) –  Jun 01 '15 at 20:24
  • There is very interesting talk by Jon Kalb about writing exception safe code in C++. He explains why exception specification is a bad thing (the idea being that can't know for sure what will thrown to your face, so you should survive against anything). Talk url: https://www.youtube.com/watch?v=N9bR0ztmmEQ (its ~3h long). – Xaqq Jun 01 '15 at 20:26

2 Answers2

5

You have to specify only checked exceptions (subclasses of the Exception class) in the method signature. Unchecked exceptions (subclasses of the RuntimeException class) don't need to be specified.

Specifying exceptions in method signature is an inherent Java practice defined by the language semantics. But there is also much controversy about it. Some teams and projects even consider it a bad practice and use only unchecked exceptions.

Generally, as a good practice, you should throw a checked exception when you define it as a part of method's contract, i.e. the method caller has to be aware of some specific (quite possible and recoverable) type of error and either catch and process it, or pass it up the call stack. Unchecked exceptions usually signify some internal error in the code of the method and thus need not be catched.

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
0

There is no profit in Java, exception specification is mandatory. Java was designed with a more strong type system than C++. In C++, exception specification was optional, and Java designers thought that exception is a important part of program design, so they decided to enforce exception specification whenever a function may throw any. This is sometimes controversial, but is a language design choice. Exception are part of calls contracts.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Exception specification is only mandatory for exceptions that are not subclasses of `RuntimeException`. – Mick Mnemonic Jun 01 '15 at 20:02
  • A stronger type system than C++. That's a bold claim that (I personally would disagree). I would consider the C++ attempt at exception specification as a failure, and in my opinion the Java versions suffers the same issues. Over time the specifications devolve into more general exceptions (as the underlying code changes over time with new different exceptions) as a result the specifications become weaker (more general) and weaker over time (on any piece of code that has regular updates). Thus making them useless (in my opinion). – Martin York Jun 01 '15 at 20:09