Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By masijade

Thread: Exception Handling

  1. #1
    bigo005 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Unhappy Exception Handling

    class Ex1 extends ArithmeticException
    {
    }
    public class ThrowDemo
    {
    public void div()
    {
    try{
    throw new Ex1();
    }
    catch(Ex1 e)
    {
    System.out.println("Caught");
    throw e;
    }
    }
    public static void main(String args[])
    {
    ThrowDemo td=new ThrowDemo();
    try
    {
    td.div();
    }
    catch(Ex1 e)
    {
    System.out.println("Main");
    }
    }
    }


    In this program the method div does not throws any exception but its still handled by the handler in main().When i tried with checked exception i didn't work error came out as exception must be caught why?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Exception Handling

    Uhm, yes it DOES throw an exception. You see that throw statement inside the catch block?
    gimbal2 likes this.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

    Default Re: Exception Handling

    Indeed. The exception is caught, but is then rethrown.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    bigo005 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: Exception Handling

    Thanks sir for reply but i was wondering in case of unchecked exception this same program shows error!

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,266
    Rep Power
    6

    Default Re: Exception Handling

    I don't understand what you are asking here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Exception Handling

    Quote Originally Posted by bigo005 View Post
    Thanks sir for reply but i was wondering in case of unchecked exception this same program shows error!
    No, yours is an unchecked exception (Runtime, error) and you are not forced to handle it. The compiler will not allow you to do with the same with a Checked
    exception. Subclasses of Exception.

    Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential Classes > Exceptions)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception Handling

    Quote Originally Posted by superhaNds View Post
    No, yours is an unchecked exception (Runtime, error) and you are not forced to handle it. The compiler will not allow you to do with the same with a Checked
    exception. Subclasses of Exception.

    Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential Classes > Exceptions)
    The RuntimeException class is also a sub class of the Exception class and it is unchecked (just as the Error class). The hierarchy is as follows:

    - Throwable
    --- Exception
    ------ RuntimeException (unchecked)
    ------ <other exceptions> (checked)
    --- Error (unchecked)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Exception Handling

    Quote Originally Posted by JosAH View Post
    The RuntimeException class is also a sub class of the Exception class and it is unchecked (just as the Error class). The hierarchy is as follows:

    - Throwable
    --- Exception
    ------ RuntimeException (unchecked)
    ------ <other exceptions> (checked)
    --- Error (unchecked)

    kind regards,

    Jos
    Yes. I know the hierarchy.

    Isn't Exception every exception's parent?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception Handling

    Quote Originally Posted by superhaNds View Post
    Yes. I know the hierarchy.

    Isn't Exception every exception's parent?
    Nope, it's the Throwable class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. exception handling
    By NoobieCode in forum New To Java
    Replies: 6
    Last Post: 05-15-2013, 01:54 PM
  2. Exception handling
    By ExX in forum New To Java
    Replies: 6
    Last Post: 03-12-2012, 12:48 PM
  3. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 07:57 PM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •