wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes true or false? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "true or false?" Watch "true or false?" New topic
Author

true or false?

andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
If there�s no code in try block that may throw exceptions specified in the catch blocks,compiler will produce an error. (This is not the case for super-class Exception)
true or false?
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
FALSE
you may not have any code in the try block and still have catch block. Compiler wojn't complain to this. You know why?
What if you want to catch run time exception?? Like this
try
{
// some array operation
}
catch (ArrayIndexOutOfBoundException ae)
{
}
Here, ArrayIndexOutOfBoundException is not a checked exception but you still want to catch it. When compiler looks at the code it cannot determine if the code in try could throw an exception (because it knows only about checked exceptions).


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Chintan Rajyaguru:
FALSE
...
Here, ArrayIndexOutOfBoundException is not a checked exception but you still want to catch it. When compiler looks at the code it cannot determine if the code in try could throw an exception (because it knows only about checked exceptions).

Correct... so if the question was reworded to:
"If there�s no code in try block that may throw a checked exception that is specified in the catch block, then the compiler will produce an error. "
The answer would be true, right?


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Yes and NO. In your catch block you could have Exception e or Throwable t and you could still be catching a runtiime exception.
Even if you have absolutely no code in try block you could catch an exception that is itself or a parent of run time exception.
What you said is also right. If try block has no code that throws a checked exception and if you try to catch checked exception, it is a compile time error and compiler will say "code unreachable".
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
"If there’s no code in try block that may throw a checked exception that is specified in the catch block, then the compiler will produce an error. "

But are there tips to determine exception type, checked or runtime? That is to say, how can I know ArrayIndexOutOfBoundsException is runtime exception?


Great thanks,<br />Luco Zhao
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
JVM knows which exception is runtime and which one is checked. If you are asking from certification exam view point, it is recommended that you look at the exception heirarchy and REMEMBER popular exceptions such as Throwable, Exception, IOException etc (I think you have to remember some of them for io package anyways.
In exam, I don't see codes expecting candidates to know which line will throw what type of exception. If you get a question that we are discussing in this thread, it would be very clear if the code throws run time or checked exception. In many cases they tell you that this code could throw XXXException
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
but how can i determine which checked exception will cause compiler fail and which not?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
A SCJP candidate needs to know that RuntimeException are so common that forcing the compiler to do the same check as it does for checked ones would be unfeasible. For instance, ArrayIndexOutOfBoundsException could occur in any access to an array. Placing all such access within a try would mess up the code.
Another way to recognize RuntimeExceptions: they generally indicate a programmer error. If the programmer would've known that he is accessing an array improperly, he won't place the array access within a try, but instead he will correct the error. Thus ArrayIndexOutOfBounsException makes no sense as checked exception.
Good news: nobody is required to determined if any exception is either checked or not, but only the most usual ones. These should be remembered thanks to their use.
[ May 12, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
how can i determine which checked exception will cause compiler fail and which not?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by andy lau:
how can i determine which checked exception will cause compiler fail and which not?

All checked exceptions must be handled or the compiler will complain. Runtime exceptions and errors are not required to be handled - those are the exceptions and errors that extends RuntimeException and Error, respectively (including RuntimeException and Error, themselves).
Corey


SCJP Tipline, etc.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Also -- you can check the API of a particular method you'll be using -- it will tell you if it throws a particular exception
Ex: from the class Integer
parseInt
public static int parseInt(String s)
throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
Parameters: s - a string.
Returns: the integer represented by the argument in decimal.
Throws: NumberFormatException - if the string does not contain a parsable integer.
--This might help:
from A Programmer's Guide to Java Certification
Except for java.lang.RuntimeException, java.lang.Error and their subclasses, all exceptions are called checked exceptions. The compiler ensures that if a method can throw a checked exception, directly or indirectly, then the method must be explicitly deal with it.

[ May 13, 2002: Message edited by: Jessica Sant ]
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
maybe i havent expressed clearly. what i mean is that:
how can i determine which checked exception in the catch declaration will cause compiler fail and which not?
thank you all the same!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by andy lau:
maybe i havent expressed clearly. what i mean is that:
how can i determine which checked exception in the catch declaration will cause compiler fail and which not?
thank you all the same!

Maybe I haven't expressed this clearly - ALL CHECKED EXCEPTIONS MUST BE HANDLED.
That means that, if a method is capable of throwing a checked exception, it must either be caught by the method that invokes it or that method must throw it. That equates to these two examples:

However, if you try to catch an exception that can't be thrown, you'll get a compile-time error.

In this example, line 1 can never be executed because the code within the try block is incapable of throwing an IOException. Therefore, the compiler will complain and tell you that you have an unreachable code segment, which you do.
So, in short all checked exceptions must be handled. However, that isn't the case for RuntimeExceptions and Errors. These need not be caught. Of course, if you don't catch them and one is thrown at runtime, your application will fail.
I hope that helps,
Corey
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
I think what Andy is trying to ask is this.
try
{
// code throwing InterruptedException from thread
}
catch (IOException ioe)
{
}
Andy's quesitons (probably): How do I know which checked exception the code in try block could throw? and weather the one being cought is the one being thrown (or superclass of it)?
Though it is obvious from this case that compiler will throw an error because InterruptedEception (checked exception) is not being cought. If you catch both Interrupted and IO exception, compiler will still complain because code is unreachable. If the question I mention above is the question Andy has, my take on it would be that you have to know all the popular exceptions and I do not think the exam will test you on this.
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Originally posted by Chintan Rajyaguru:
FALSE
you may not have any code in the try block and still have catch block. Compiler wojn't complain to this. You know why?
What if you want to catch run time exception?? Like this
try
{
// some array operation
}
catch (ArrayIndexOutOfBoundException ae)
{
}
Here, ArrayIndexOutOfBoundException is not a checked exception but you still want to catch it. When compiler looks at the code it cannot determine if the code in try could throw an exception (because it knows only about checked exceptions).

This Compiles:

There are no executable statements in the try block so how can the ArrayIndexOutOfBoundsException be thrown?The catch block is unreachable so why is this not a compile error?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Because ArrayIndexOutOfBounds is a RuntimeException - it's not a checked exception. You can do this with RuntimeExceptions only. If you try to do it with a checked exception(ie, any Exception that is not a RuntimeException), you will get a compiler error.
Try it. Replace ArrayIndexOutOfBoundsException with IOException and see if it still compiles.


Rob
SCJP 1.4
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Originally posted by luco zhao:

But are there tips to determine exception type, checked or runtime? That is to say, how can I know ArrayIndexOutOfBoundsException is runtime exception?

http://java.sun.com/j2se/1.3/docs/api/java/lang/RuntimeException.html
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Originally posted by Rob Ross:
Because ArrayIndexOutOfBounds is a RuntimeException - it's not a checked exception. You can do this with RuntimeExceptions only. If you try to do it with a checked exception(ie, any Exception that is not a RuntimeException), you will get a compiler error.
Try it. Replace ArrayIndexOutOfBoundsException with IOException and see if it still compiles.

Thanks now checked vs unchecked is checked off my to learn list

no compile error:

compile error:
trying to catch a checked exception that is not generated:

trying to catch an exception that has allready been caught:

finally unreachable code:
 
jQuery in Action, 2nd edition
 
subject: true or false?
 
Similar Threads
plz need help
You can directly run the GC whenever you want to ?????
Short Circuit AND
Double doubt
only static methods can access static members