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 3 of 3

Thread: Try-Catch Statement & Exceptions

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Try-Catch Statement & Exceptions

    Hi,

    Can try-catch statement can only be used to handle checked exceptions?

    &&

    Are exceptions objects?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Try-Catch Statement & Exceptions

    Quote Originally Posted by jamesh View Post
    Can try-catch statement can only be used to handle checked exceptions?
    A catch can handle any type of exception, checked or unchecked.

    Quote Originally Posted by jamesh View Post
    Are exceptions objects?
    Yes, apart the fact that an exception is in a well defined hierarchy (with java.lang.Throwable at the "top"), an exception is an object like others. When you throw an exception you typically do something like:

    throw new XyzException();

    Thus, you first instantiate the exception like any other class.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following 2 Users Say Thank You to andbin For This Useful Post:

    GregBrannon (January 3rd, 2014), jamesh (January 3rd, 2014)

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    232
    My Mood
    Stressed
    Thanks
    36
    Thanked 66 Times in 54 Posts
    Blog Entries
    4

    Default Re: Try-Catch Statement & Exceptions

    Quote Originally Posted by jamesh
    Are exceptions objects?
    Yes. Read up on them a little at Essential Classes > Exceptions.

    Can try-catch statement can only be used to handle checked exceptions?
    I'm not sure what you mean by 'checked exceptions' but if you remove the word 'checked' then yes. The purpose of a try-catch block is to react to code that may throw an exception.

    try {
            // Some code that throws an exception
            // Note that if an exception *is* thrown at some point
            // it doesn't execute any more code from this block
     
    } catch (NameOfTheException) {
           // Do something when this type of exception is thrown
     
    } catch (Exception) {
           // Exception is the superclass of all exceptions.  
           // You can even create your own exceptions by extending this class 
           // and then throw it where appropriate
     
    } finally {
          // This happens weather an exception is thrown or not.
     
     
    }

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    jamesh (January 3rd, 2014)

Similar Threads

  1. How to catch exceptions in Java
    By brajraj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 4th, 2012, 08:55 AM
  2. Examples of Try Catch Exceptions
    By ch103 in forum Exceptions
    Replies: 11
    Last Post: February 8th, 2012, 08:22 PM
  3. Always Catch Exceptions
    By Tjstretch in forum Java Programming Tutorials
    Replies: 2
    Last Post: October 27th, 2011, 02:26 PM

Tags for this Thread