File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception interview question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Exception interview question" Watch "Exception interview question" New topic
Author

Exception interview question

Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 31
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
Bartender

Joined: Jan 28, 2008
Posts: 4503

moving to Beginning Java


Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4503

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

Joined: Mar 18, 2011
Posts: 177

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
Ranch Hand

Joined: Dec 08, 2010
Posts: 674

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 31
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

Joined: Mar 17, 2011
Posts: 1610

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


More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8428

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?


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
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

Joined: Mar 18, 2011
Posts: 177

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

Joined: Jan 03, 2004
Posts: 3143
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.
 
 
subject: Exception interview question
 
Threads others viewed
Checked exceptions
exceptions
try...catch block
explain try catch problem
one more question on arrays only
developer file tools