• 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

Exception and its subclass

 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends..

Why this code doesn't error?..


And then i create my own exception which extend java.lang.Exception, but its error..


Why compiler tells error with ErrorClass1, and compiler doesn't tell error with RuntimeException, whereas RuntimeExeption is a subclass of java.lang.Exception?..

Thanks..
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote from Java Language Specification


It is a compile-time error if a catch clause catches checked exception type E1 but there exists no checked exception type E2 such that all of the following hold:

  • E2 <: E1
  • The try block corresponding to the catch clause can throw E2
  • No preceding catch block of the immediately enclosing try statement catches E2 or a supertype of E2.

  • unless E1 is the class Exception.



    System.out.println doesn't throw any checked exception

    ErrorClass1 is checked exception but any statement in try block doesn't throw ErrorClass1 (or its subclass), so this is compile-time error
    RuntimeException is unchecked exception so it doesn't invoke compile-time error
     
    Ranch Hand
    Posts: 504
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    this topic has been discussed this days on ranch like here
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Problem Solved:


    in your example you are voilated the hierarchy of catching the exception .
     
    Leonardo Carreira
    Ranch Hand
    Posts: 494
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Sorry, i still got error although i have changed code with the sequence that you gave..
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry!. Order doesnt matter since ErrorClass1 and Runtime both are direct subclass Of Exception . the thing is that you need to throw the ErrorClass1 exception[Checked Exception] from your try block
     
    Leonardo Carreira
    Ranch Hand
    Posts: 494
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Both of RuntimeException and ErrorClass1 extends java.lang.Exception directly..
    as we know that RuntimeException is Unchecked-Exception, and its inherited from java.lang.Exception..

    i want to make ErrorClass1 as a Unchecked-Exception, can i reach it?..
    Since, the hierarchy of RuntimeException and ErrorClass1 is same (extends java.lang.Exception directly)..

     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Leonardo Carreira wrote:i want to make ErrorClass1 as a Unchecked-Exception, can i reach it?..



    Extend RuntimeException instead of Exception
     
    Leonardo Carreira
    Ranch Hand
    Posts: 494
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    seetharaman venkatasamy wrote:
    Extend RuntimeException instead of Exception



    Yes, that's the answer..
    but why while we extends Exception directly it tells error, whereas RuntimeException itself extends Exception..
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    http://www.jguru.com/faq/view.jsp?EID=64290
     
    reply
      Bookmark Topic Watch Topic
    • New Topic