-7

I am confused to get a clear understanding of when throw is used and throws is used. Kindly provide me an example to show the difference.

Also, I tried the below code:

package AccessModifiers;

//import java.io.IOException;

public class ThrowExceptions {

int QAAutoLevel;
int QAExp;

void QAAutomationHiring(int grade)
{
    if (grade<5)
    throw new ArithmeticException("Not professionally Qualified");  
    else
        System.out.println("Ready to be test");

}

void QAExperience(int x,int grade)
{

        QAAutomationHiring(grade);


}

void checkThrowsExep(int a,int b) throws ArithmeticException
{
    try{
    int result=a/b;
    System.out.println("Result is :"+result);
    }
    catch(Exception e)
    {
        System.out.println("The error messgae is: "+ e.getMessage());
    }
}

public static void main(String args[])
{
    ThrowExceptions t=new ThrowExceptions();
    //t.QAAutomationHiring(8);
    t.QAExperience(2,8);
    t.QAExperience(4,2);

    t.checkThrowsExep(5, 0);

}

}

In the above code, when I run the program, the line- 't.checkThrowsExp' in main function is not reached. I studied that throw and throws are used to catch the exception and continue with the program execution. But here the execution stops and not proceeding to the next set of statements. Please share your comments.

Vandee
  • 19
  • 1
  • 2
  • 4
  • 1
    `throw` is used when you want to throw an exception. `throws` declares for a method what potentional `Exceptions` are thrown in order for the caller to know what to catch. – SomeJavaGuy Aug 15 '16 at 07:15
  • 3
    See [Oracle Java Tutorials - Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/) – Jesper Aug 15 '16 at 07:16

2 Answers2

3

throws is used to tell people that

Warning: this method/constructor has a high chance of throwing XXXException and YYYException! Please make sure you handle them!

Example:

The Thread.sleep method is declared as:

public static native void sleep(long millis) throws InterruptedException;

As you can see, the throws keyword tells people that sleep is very likely to throw an InterruptedException. Because of this, you must surround the method call with try-catch or mark the caller method with throws InterruptedException. The exceptions after the throws keyword are usually "checked" exceptions which are caused by invalid conditions in areas outside the immediate control of the program, like invalid user inputs, database problems etc.

Please note that it is possible for a method marked with throws XXXExcepion to never throw XXXException.

throw, on the other hand, actually throws the exception. It can be used like

throw new RuntimeException("Something went wrong!");

And whenever code execution reaches this statement, an exception will be thrown no matter what, and the method returns.

In short, throw actually does the throwing, and throws is only saying that an exception will likely be thrown (which in fact, be wrong).

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Throw actually returns the exception whereas throws is a sign to the compiler, that this method could return an exception.

In your code above the exception ArithmeticException will be created and returned, if the grade is lower than 5, which is the case in your second call of QAExperience. As the calling method called the method, which returned the exception, is not insede a catch block it will also just stop its execution and return to the main method. As the main method also does not catch the exception it will just like the others stop its execution and return the exception. This is the reason, why t.checkThrowsExp will not be executed.

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • Thank you so much! I tried calling the checkThrowsExp function before the QAExperience function in main and i got the message displayed for ArithmeticException. – Vandee Aug 15 '16 at 08:10
  • Also, i tried adding try and catch block around the throw exception in QAAutomationHiring function. This displays the exception message and continues with the next step of statements. Now i get the point. – Vandee Aug 15 '16 at 08:13