Results 1 to 4 of 4

Thread: Difference between throw and throws in Java Exception Handling

  1. #1
    Join Date
    Sep 2009
    Posts
    177

    Difference between throw and throws in Java Exception Handling

    I am learning Java. I reached the topic of exception handling in Java. I am not able to understand the exact difference between throw and throws with respect to exception handling in Java. What is the actual difference between the two ? What are the funtionality of each individual ? I am totally lost in this topic.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Difference between throw and throws in Java Exception Handling

    The keyword throw and throws are used for Exception handling in Java. Sun Microsystems developed Java has many feature of which is Exception Handling. Precisely a throw statement initiates an exception to be thrown, where as the throws clause declares that the method is expected to throw one or more checked execptions. Thus both are different and perform two different task.
    Last edited by Modifier; 09-11-2009 at 10:38 PM.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,394

    Re: Difference between throw and throws in Java Exception Handling

    In very simple words, "throws" declares that the method is capable of throwing an exception while "throw" actually does the job of throwing the exception. They are not same.
    eg:
    Code:
    public void Exep(int param) throws MyException
    {
    
    if (param > 7)
    {
    throw new MyException("You have entered big value");
    }
    .
    .
    .
    }
    Consider the above code. The keyword throws signifies that the method MyException is expected to throw exception and the keyword throw will actually throw the exception.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,949

    Re: Difference between throw and throws in Java Exception Handling

    The keyword throw in Java is used to raise exception explicitly that means it is used when a user defined exception is raised.
    The keyword throws in Java is a modifier of a method that is expected to generate and throw exception/s. Mostly methods which are capable of throwing an exception but not handling are specified with throws keyword.

Similar Threads

  1. AxAcroPDF throws unhandled exception on click of TAB
    By Who is it in forum Windows Software
    Replies: 5
    Last Post: 14-07-2010, 03:38 AM
  2. Exception handling in PL/SQL
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:04 PM
  3. Java : Difference between the Checked and Unchecked Exception
    By Rajeev D in forum Software Development
    Replies: 4
    Last Post: 08-02-2010, 05:17 PM
  4. Svchost.exe Throws Exception in Windows xp
    By KomalUpadhyay in forum Operating Systems
    Replies: 3
    Last Post: 11-12-2009, 12:07 AM
  5. How to throw a custom exception in .NET?
    By Paramartha in forum Software Development
    Replies: 3
    Last Post: 11-09-2009, 11:18 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
  •