• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

RuntimeException v/s IllgealAccessException ?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a program A :
class A {
static void demoproc() {
try {
System.out.println("inside demoproc: ");
throw new RuntimeException("demo");
} finally {System.out.println("finally in A "); }
}
public static void main(String args[]){
try {
demoproc();
} catch(RuntimeException e) {
System.out.println("caught ");
}
}
}

I've program B like :
class B {
static void demoproc() {
try {
System.out.println("inside demoproc: ");
throw new IllegalAccessException("demo");
} finally {System.out.println("finally in A "); }
}
public static void main(String args[]){
try {
demoproc();
} catch(IllegalAccessException e) {
System.out.println("caught ");
}
}
}
The program "A" works, but "B" end in compilation errors, the only difference between the above two is "RuntimeException" and "IllegalAccessException", why is that? Could anyone explain that please??
Vijay
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuntimeException is a runtime exception
that means that if u want to throw one of those u dont need to declare it in the method that throws it.
however IllegalAccessEXception is a checked exception.
that means that the method that throws one of those need to declare it.
add this to the method declaration:
static void demoproc() throws IllegalAccessException
then it will compile and run.
(notice the s in throws ) which means we just delaring to the compiler that this method may throw an exception of that kind.
 
get schwifty. tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!