• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception interview question  RSS feed

 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have some interview question asked today can anyone reply me...
first class
-------------------



second class
----------------


Above 2 classes what is the output for 2 classes and why??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moving to Beginning Java
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all the both idiom is wrong since you cant define try catch outside a block.

and Guess interviewer may be interested to know the difference about catching an exception and catching an error. read the java doc about java.lang.Error
<edit>your question is missing clarity though...</edit>
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First we don't catch any errors in java. some times we may catch specific errors(based on requirement)
second as of my knowledge the try catch block should be inside a method othe wise it is jvm consider it as unexpected token try
if i am wrong please correct me someone..
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,

What do you mean by output of two classes? I don't see any methods etc. As already mentioned in previous replies, try-catch block must not be directly inside a class (but inside a method).

If this is the exact question asked, then answer is compile time error.
 
Ravi kapa
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is right and i mean that is there any compilation time error in those classes(eventhough try and catch inside a method)........
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:Question is right and i mean that is there any compilation time error in those classes(eventhough try and catch inside a method)........

But they aren't inside a method, so either the question is NOT right, or you have already been given the answer.

I suggest you provide us with the classes and question exactly as they were given in the interview.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:is there any compilation time error in those classes(eventhough try and catch inside a method)........

ummm...what happened when you tried compiling them? Wouldn't that give you the definitive answer?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun dontamsetti wrote:First we don't catch any errors in java. some times we may catch specific errors . . .
What is an error? Do you mean an Error? That is something different because of the capital E. You need to be very careful about such spellings.

You are right; you ought not to catch Errors. you should also catch a specific kind of Error and re-throw it, if you ever do catch Errors.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for misspell it's Error only. I hear that some frameworks are catching errors like "StackOverflowError"

not only this but also kind of these. is it good thing to catch Errors in java?(Documentation recommended better leave it to JVM as i understood).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun dontamsetti wrote:
not only this but also kind of these. is it good thing to catch Errors in java?(Documentation recommended better leave it to JVM as i understood).


In general, no, we do not catch Error or its subclasses. However, there can be exceptions to that rule. For instance, if you are developing an app server or other managed application container, like JBoss or WebSphere, if one of the applications throws an OutOfMemoryError or StackOverflowError, you might not want that to be able to kill all the other apps running in that container. So the container may catch Errors throws by the individual apps, log them, and let that app die, and hope that other apps can continue running.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!