Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: why should we declare checked exceptions?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default why should we declare checked exceptions?

    Hi !My doubt is on checked,unchecked exceptions in java.
    1)You know that if there is any possibility for checked exception to be thrown from a method and we do not have intention to handle the thrown exception then we have to declare that exception in method header.That means we have to write “throws EXCEPTION NAME” in method header.
    2)But in case of unchecked exception, if there is any possibility for unchecked exception to be thrown from a method then we do not declare that exception in method header.. That means we do not write “throws EXCEPTION NAME” in method header .
    3)Why should we declare the exception in method header in case of checked exceptions ? what benefits are we getting by declaring exception in method header?
    4)Why do not we declare the exception in method header in case of unchecked exceptions?


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why should we declare checked exceptions?

    in case of checked exceptions-it follows throw early catch later principal which is necessary for handling the exceptions.
    in case of unchecked exceptions-we donot know the type of exception, they occur at runtime. so we cannot throw the unchecked exceptions.

  3. #3

    Default Re: why should we declare checked exceptions?

    The Exception which are checked by compiler for smooth execution of the program at runtime are called Checked Exception. In the case of Checked exception, compiler will check whether we are handling the exception if not then we will get the compiler error that means checked exceptions are subject to the Catch or Specify Requirement.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: why should we declare checked exceptions?

    3)Why should we declare the exception in method header in case of checked exceptions ? what benefits are we getting by declaring exception in method header?

    Checked exceptions must be caught somewhere within your application. If the function is not able to properly deal with the exception, it should not catch it, but has to let it propagate up the call stack to someone who does catch it. That's the reason for "throws Exception", to propagate the exception up the call stack instead of dealing with it right there.

    4)Why do not we declare the exception in method header in case of unchecked exceptions?

    It's mostly for programmer convenience. It would be horrible to have to try/catch everything that could possibly be null, for example. Unchecked exceptions get caught by the JVM and printed to System.err.

Similar Threads

  1. Suggestion to declare variable that is declared
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2013, 06:32 PM
  2. How to declare byte enum
    By alex007 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2012, 05:51 AM
  3. See if checkbox is checked
    By kney in forum Java Theory & Questions
    Replies: 9
    Last Post: December 7th, 2011, 07:23 AM
  4. caught or declare might be thrown error
    By IDK12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2011, 01:55 PM
  5. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM