• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions ExamLab 6.0

 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ExamLab 6.0 does not allow user to save the test but I vaguely remember some question like this:



In such a case, why is there an compilation error at line 5 and not line 7. What is the logic used?
 
Mohamamd Amin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code within the try and catch statement does not throw InterruptedException. There will be no compilation errors when you remove the following statement:
catch (InterruptedException e) {
}

because catch(Exception e) will catch the exception and the program will terminate normally.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
InterruptedException is a subclass of Exception so why does the compiler "pick" on InterruptedException and not on Exception
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vyas Sanzgiri wrote:InterruptedException is a subclass of Exception so why does the compiler "pick" on InterruptedException and not on Exception

Because the Java specification allows to catch Exception (but not specific checked exceptions.) The point is that you could catch a runtime exception with an Exception catch. And, the compiler can only enforce this rule for checked exceptions, because they are the only ones that methods are forced to declare.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
point noted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic