• 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

checked exception

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the following code will compile; but isn't IndexOutOfBoundsException a subclass of RunTimeException, therefore it's not a checked exception ?

so why the following code compile ?

Please help to explain.


   catch (InputMismatchException[/i]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:
so why the following code compile ?



Well, obviously, you were expecting the code not to compile... but I don't seem to follow your reasoning.  What does the exception being an unchecked exception, mean that the code should not compile?  ... and also, I am assuming the other exception code oddity is caused by a bad cut-n-paste.

Henry
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A note about your post: please UseCodeTags (that's a link) when you are posting code.  I've done it for you this time.  I also assumed (like Henry) that the last line is a mistake.
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the editing.  I'll look into it.

My reasoning is that unchecked are not actually catched exception by Java , it'll never be catched, but will be a runtime error. Otherwise, why Java make the distinction between error, runtime error and checked
exceptions ? Isn't it misleading to have the code compiled but not actually performed ?

 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

May be only checked exception such as "IOException" for Files class, by calling method:
public static Path createTempFile(String prefix,
                                 String suffix,
                                 FileAttribute<?>... attrs)
                          throws IOException
must be catch, otherwise won't compiled.

But Java code can catch any of the 3 times of exception.
Yes, that's must be case, but I'd like to hear someone else confirm this.
Thanks.
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The called Java method can throws any type of exception, and the calling method must catch it or throw it.
I read somewhere that Java has 2 types of exception: "checked" and "unchecked" and that "checked" must be subclass of Exception but not subclass of "RuntimeException".
This was the reason for my original post.

Now, I think the the "checked" exception is referring to the JDK methods that has "throws" clause.






 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:
My reasoning is that unchecked are not actually catched exception by Java , it'll never be catched, but will be a runtime error. Otherwise, why Java make the distinction between error, runtime error and checked exceptions ? Isn't it misleading to have the code compiled but not actually performed ?



A checked exception is an exception that is checked by the compiler. The compiler will check to confirmed that it is actually thrown (or declared as thrown), and if it is, it will also confirm that it is handled or declared as thrown by the caller.

An unchecked exception is an exception that is *not* checked by the compiler. The compiler makes no assumptions regarding it.... so, it can't tell if it is actually thrown or not.

Henry
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm more confused than was before.  
The following code will compile. But it calls  Boolean kh = Array.getBoolean(value, 5) which has this method signature:


[/color]
which make me think, caller must catch or throw !!! otherwise it should not compile


 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:I'm more confused than was before.  
The following code will compile. But it calls  Boolean kh = Array.getBoolean(value, 5) which has this method signature:
[**Code Deleted**]
which make me think, caller must catch or throw !!! otherwise it should not compile



Declaring the "throws" of an unchecked exception, does *not* make it a checked exception. The exception is still unchecked, and hence, the compiler does not bother to check whether it will be handled, thrown, or declared as thrown.

The "throws" declaration (in this latest example) does not serve a purpose for the compiler. I believe the main purpose of the declaration is for the generation of the java documentation.

Henry
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Definition of "checked" exception: subclass of Exception but not subclass of RuntimeException.

"checked" exception must be caught or thrown to be compiled; while "unchecked" exception has option to be caught or thrown for successful compilation.
"unchecked" has option to be caught or thrown just like "checked" exception and if caught or throw will work just like "checked" exception.

Thank you for your reply and if this is not right, please reply.




 
Greenhorn
Posts: 5
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it compiles because it is not forbidden to catch runtimeexceptions.

Ketung Xiao wrote:the following code will compile; but isn't IndexOutOfBoundsException a subclass of RunTimeException, therefore it's not a checked exception ?

so why the following code compile ?

Please help to explain.


   catch (InputMismatchException[/i]

 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marius Zilenas wrote:I think it compiles because it is not forbidden to catch runtimeexceptions.

Agree, and if the code catch runtimeexceptions or "uncheck" exception which include error exception, it will work just like "checked" exception.

 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I meant to include your quote: I think it compiles because it is not forbidden to catch runtimeexceptions.

I Agree, and if the code catch runtimeexceptions or "uncheck" exception which include error exception, it will work just like "checked" exception.

By the way, is there a way to delete my own quote after I submit a post ?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:. . . it is not forbidden to catch runtimeexceptions.

You can catch any kind of exception with a small e. If you look in the Java® Language Specification (=JLS), it tells you that,

An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.

Throwable and all its subclasses are, collectively, the exception classes. . . .

Beware: the JLS can be difficult to read. If you look up the details of IndexOutOfBoundsException, you will see it is a subclass of Throwable, so it counts as an exception class (small e).

. . . if the code catch runtimeexceptions or "uncheck" exception which include error exception, it will work just like "checked" exception.

That doesn't look correct, I am afraid. Every instance of all exception classes can be caught, but as Henry has already said, you can only catch a checked subclass of Exception if it can actually be thrown in the try block.

By the way, is there a way to delete my own quote after I submit a post ?

No. Old posts can be useful and we often see people benefiting from posts over ten years old
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you are say: IndexOutOfBoundsException which is subclass of java.lang.RuntimeException CANNOT be caught or thrown ?
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IndexOutOfBoundsException
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:So, you are say: IndexOutOfBoundsException which is subclass of java.lang.RuntimeException CANNOT be caught or thrown ?



The IndexOutOfBoundsException is an unchecked exception, and as already been mentioned, the compiler does *not* check these exceptions. This means that the compiler will *not* prevent you from trying to catch (or throw) these types of exceptions.

Henry
 
Ketung Xiao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I reset the email notification request  back to 'YES' on this thread ?
I inadvertently click on the email link to turn off email notification on this thread.


The following code will run:

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:So, you are say: IndexOutOfBoundsException . . . CANNOT be caught or thrown ? . . .

No, I said that all exception classes' objects can be caught and the IndexOutOfBoundsException is an exception class.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ketung Xiao wrote:
The following code will run:


(Well, it will if you add the correct import statements.)

Are you surprised that it runs?  Why?  Maybe re-read the replies in the topic before replying.
reply
    Bookmark Topic Watch Topic
  • New Topic