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

Thread: Reserved word "throws"

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reserved word "throws"

    Heyey everyone. I've read many articles about handling exception but some do not discuss throws briefly and others do not discuss em at all.

    I get the idea of try/catch blocks and somewhat of the reserved word "finally."
    What i don't get is why one does not need the catch block when one adds throws Specific Exception or Exception in the main method. What happens when the specified exception occur? Explicitly, what does the reserved word throws do?

    Any help is appreciated.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reserved word "throws"

    Throw means that at that point, an object (almost always an Exception) gets thrown, and the program skips over all other code until that object is caught.

    There are two types of Exceptions: checked and unchecked. Basically, the difference is the compiler forces you to catch checked exceptions and doesn't for unchecked exceptions. If a method is declared that it throws a certain type of checked Exception (it can be declared to throw unchecked exceptions, too but it doesn't force anyone to catch them), it simply means that the method doesn't have to catch that Exception if it's thrown somewhere inside, but who-ever is calling that method must catch it (unless that method is declared to throw that same type of exception, too).

    If you declare your main method to throw Exception, that means it doesn't have to catch it. The JVM will automatically catch all exceptions that come back to it, i.e. any that weren't caught and have percolated up past the main method.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM

Tags for this Thread