Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

not understand ( Exception )

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! This class generates an error in line 3,4,6 did not understand
because, somebody could help me?
Thanks!!
Oelison [ BRAZIL ]
1. class A {
2. void m1() {throw new IndexOutOfBoundsException();}
3. void m2() {throw new InstantiationException();}
4. void m3() {throw new IllegalAccessException();}
5. void m4() {throw new NullPointerException();}
6. void m5() {throw new NoSuchMethodException();}
7. void m6() {throw new SecurityException();}
8. }
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe those exceptions are checked exceptions, so the methods have to declare to throw those exceptions before they can "throw"
the following code would work:
class A
{
void m1() {throw new IndexOutOfBoundsException();}
void m2() throws InstantiationException {throw new InstantiationException();}
void m3() throws IllegalAccessException {throw new IllegalAccessException();}
void m4() {throw new NullPointerException();}
void m5() throws NoSuchMethodException {throw new NoSuchMethodException();}
void m6() {throw new SecurityException();}
}
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method that throws a RuntimeException is not required to list the exception in the throws clause of the method declaration. A method that throws a checked exception is required to list the exception in the throws clause of the method declaration.
In this question, the method declarations that generate compile-time errors fail to list the checked exception in the throws clause.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the javadoc for these exceptions. You'll see that the ones that cause compiler errors extend Exception, whereas the ones that are OK extend RuntimeException.
Java has two types of Exception (and one type of Error), checked exceptions and runtime exceptions. Checked exceptions extend Exception, and must either be thrown explicitly by a method or handled within it using a try...catch block. The example code on lines 3,4 and 6 does neither and so the compiler generates an error. Runtime exceptions extend RuntimeException, and do not need to be handled explicitly.
A good way to think of it is that runtime exceptions come from bugs in the code (IndexOutOfBounds, for example) while checked exceptions can happen within bug-free code (maybe a network resource is unavailable). The compiler makes sure that your code copes with the latter case by making your method either catch the exception internally or explicitly throw it to the calling method.
HTH
(edit: wow, two other people replied while I was typing )
Ben.
[ December 19, 2002: Message edited by: Ben Ritchie ]
 
Calvin Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we are at it, does anybody know if SCJP test for identifing which exception is checked and which one is not? Do I need to remmber all those types or the general rule of thumb (bug vs. expected exception) would be enough?
Thanks!
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Oelison"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Ben Ritchie
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calvin
I don't think you need too learn individual exceptions - I never did, although I know some obvious ones (e.g. index out of bounds) through experience. I cannot remember anything requiring you to memorize exception types appearing in the exam (I would have got it wrong ), so understanding the general rule should be fine.
I would expect them to give you a simple inheritance diagram (FooException extends Exception) and then test your understanding based on that.
 
Calvin Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben!
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!