• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Exeptions - What is Wrong with this code.

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Strings{

public void f() {

try {
throw new RuntimeException();
}
finally {
System.out.println("CAUGHT1");

}
System.out.println("CAUGHT1");
}

public static void main(String args[]) throws Exception{
Strings t = new Strings();

try{

t.f();
System.out.println("MAIN");
}
finally{}
System.out.println("CAUGHT0");
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile and run it?

If the result is different from what you thought it would be, let us know what you were expecting, and we'll try to sort it out.
[ August 04, 2007: Message edited by: Ulf Dittmer ]
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code dosen't compile, it states unreachable code at line specified below


class Strings{

public void f() {

try {
throw new RuntimeException();
}
finally {
System.out.println("CAUGHT1");

}
System.out.println("CAUGHT1"); // Unreachable code.
}

public static void main(String args[]) throws Exception{
Strings t = new Strings();

try{

t.f();
System.out.println("MAIN");
}
finally{}
System.out.println("CAUGHT0");
}
}
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude if take a look at this code, both are logically same, but this works fine.


public void f() {

try {
int x= 10/0; // REPLACED RUNTIME EXCEPTION WITH THIS
}
finally {
System.out.println("CAUGHT1");

}
System.out.println("CAUGHT1");
}

public static void main(String args[]) throws Exception{
Strings t = new Strings();

try{

t.f();
System.out.println("MAIN");
}
finally{}
System.out.println("CAUGHT0");
}
}
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dude if take a look at this code, both are logically same, but this works fine.



Maybe so, but the compiler is not smart enough to know that. In the first case, it does know that the exception will be thrown, and hence, the code after the try-finally block is not reachable. In the second case, it is just an arithmetic expression, that is not checked for runtime errors during compile time.

Henry
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you. But 10 and 0 are literals.So the compiler should not skip that step.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but the compiler knows, that it will throw a RuntimeException, not a checked exception. Therefore no handling (catch or declare) is required.

Bu.
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude,
If the complier knows then why isn't it complaining at compile time.If you analyze the following snippets,

Snippet 1:
-----------
int x = 10/0; // This code block is for sure to throw an error. So issues should be resolved at compile time.

Snippet 2:
-----------
int x,y
int z = x/y; // The value of Y cannot be determined till run time.So it make sense to throw a runtime exception, but not in the former case.


If the compiler sits quiet then it should do the same for

throw new RuntimeException();


For the complete code please refer to starting posts.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the complier knows then why isn't it complaining at compile time.If you analyze the following snippets,



Because the compiler doesn't know. It knows that it is a literal zero, and it knows that it will divide them, but there is no check at compile time to determine whether it may throw an exception at runtime. This may change in the future, as the compiler improves, but currently, it doesn't make such a check.

Henry
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic