• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bizarre o/p?

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test6{
public static void main(String args[]) throws Exception{
try{
throw new Exception();
}
finally{
System.out.println("No Error");
}
}
}

hi frnds,

when i run this code it is giving me sometimes
No error followed by java.lang.Exception and sometimes

java.lang.Exception followed by No error

in the mock exam they have given both the answers and given the correct answer as No error followed by java.lang.Exception ....

one more doubt plz...

can i say that if i find anything like public static void main(String[] arg) throws Exception

then can i say that runtime exception occurs in main thread .....

thanks & regards

srikanth
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you running the program from the command line? Or are you using an IDE? Using Java 5.0 from the command line, I consistently get the expected result:
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srikanth-


can i say that if i find anything like public static void main(String[] arg) throws Exception

then can i say that runtime exception occurs in main thread .....



I believe it's valid to say that any exception that is not caught occurs in the main thread. This would apply to checked exceptions, unchecked exceptions and errors.

Also, the syntax that has the main method throwing an exception like this...



...is unusual and probably redundant, but is valid nonetheless. I got bit on that on a number of questions where I assumed that the code wouldn't compile, but later found out that it's perfectly valid for main to throw exceptions.

Hope that helps,
Josh
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI BARRY ,

iam using IDE and not command line ..is this the problem .

thanks & regards

srikanth
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"No Error" is printed to System.out and after it the exception is printed to System.err.
Some IDEs may delay the No Error message on System.out so it appears after the exception message. (Becuase they are on different output streams.)

After changing

to


"No error" should always appear before exception message.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Vlado;

thanks & regards

srikanth
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Vlado;

thanks & regards

srikanth
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic