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.

 


11 thoughts on “Difference between throw and throws keyword with example in Java”

  1. Dear Friend,

    actually everybody post java program same king of way you also follow same but your explanation is very clear. Nobody did not post Application of Java Concept. for example where we use exception handling, when it need?. what kind of situation we are going to use? suppose you are doing any project all the time you don’t use this technique. so try to post situation and techniques. thank you very much for your informations. suppose any mistake to me please forgive me…

Add Comment Register



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>