Thorow vs Throws- Clarifications required | Selenium Forum
M
Posted on 15/08/2016
Sir,

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. On the video you have explained 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 clarify.

Thanks,
Vandhana

M
Replied on 16/08/2016

i'm not able to understand what you are asking.

can please try again.