Difference between throw and throws keyword with example in Java



Difference between throw and throws keyword with example in Java. Throw and Throws are the two keyword used in java for exception handling. To know this keyword, we should know about exception and why to handle exception, and how we can handle exception in java. Here is the definition of throw and throws :

Difference between throw and throws keyword with example in Java

Throws: Throws declares that your method is capable of throwing an exception but it does not handle the exception that must be specified by using “throws” class.

Throw:Throw keyword does work to throw an exception. When a user defined exception is to be raised, throw keyword is used.

Difference between throw and throws keyword with example in Java

Difference between throw and throws keyword with example in Java

Let us understand why and where to use throw and throws keyword, so as to understand the actual definition and difference between throw and throws.

Difference between throw and throws keyword with example in Java

Throws : Why throws? When programmer defines a method and he knows that some part of code can raise an exception, and he don’t want to handle that exception within that function, then the method is defined with throws keyword as :

void play() throws NullPointerException;

Now when another programmer wants to use play() function, he needs to call that method in try block and a catch block should be defined by the programmer to handle that exception. Let us understand by the example:

//Demonstrate throw

class ThrowDemo1
{
	static void play()  throws Exception
	{
            System.out.println("before");
            throw new IllegalAccessException("demo");
            //System.out.println("after");

	}

        public static void main(String args[])
        {
            try
            {
		play();
            }
            catch(Exception e)
            {
		System.out.println("caught  me: " +e);
            }
        }
}

Output of Difference between throw and throws keyword with example in Java:

Difference between throw and throws keyword with example in Java

Difference between throw and throws keyword with example in Java

Throw : Why throw? Throw keyword is mostly used to throw the exception to raise user defined exception i.e if user define its own exception by extending the exception class, then that exception can be raise by only using throw keyword. Throw keyword should be defined in either try catch block or the method should be defined using throws keyword. Here is an example :

//Demonstrate throw

class ThrowDemo2
{
    static void play()  
    {
        try
        {	
            System.out.println("before");
            throw new IllegalAccessException("demo");
            //System.out.println("after");
        }
        catch(Exception e)
        {	
            System.out.println("Exception e"+e);
        }
    }

    public static void main(String args[]) throws Exception
    {
        play();
    }
}

Output of Difference between throw and throws keyword with example in Java:

Difference between throw and throws keyword with example in Java

Difference between throw and throws keyword with example in Java

Just try to make changes in above program and run it and see the result. This will clear more difference. I recommend you to take more look on books which explains throw and throws keyword difference. If you need to discuss more about this topic, comment on below.

 




"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in Computer Concept, java, Language and tagged , , , , . Bookmark the permalink.

3 Responses to Difference between throw and throws keyword with example in Java

  1. Habitamu says:

    I like it.continue such like small but have very good/great knowledge giver notes.

  2. Anbu says:

    well explained, easy to understand… thank you .. looking forward..

  3. dR.k9 says:

    Awesome man , love this website , definitely learnt something , keep up your good work ! :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>