my dog learned polymorphism*
The moose likes Beginning Java and the fly likes RuntimeException v/s IllgealAccessException ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RuntimeException v/s IllgealAccessException ?" Watch "RuntimeException v/s IllgealAccessException ?" New topic
Author

RuntimeException v/s IllgealAccessException ?

vijay gardiner
Greenhorn

Joined: Jan 26, 2002
Posts: 14
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
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RuntimeException v/s IllgealAccessException ?
 
Similar Threads
Do Static methods throw exceptions?
Exception doubt
Compile Error
throw an Exception....
Throw - what's the point??