• 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

true or false?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"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?
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how can i determine which checked exception will cause compiler fail and which not?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i determine which checked exception will cause compiler fail and which not?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Charlie Sturman
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic