Javatpoint Logo

what is the difference between throw and throws pls explain with simple program

By: jittec*** On: Mon Nov 11 08:22:53 EST 2013     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
i am leaner through your javatpoint
Up0Down

 
"throw" is a keyword for creating our own Exception or CustomException.
for Example if you are going for ATM you need 10000 bugs, but your account has 1000 bugs then if you are not using this "throw" it will show "404 Server Error".Instead of displaying server specific errors use this "throw" keyword.

Ex:
class ThrowDemo{

static void validate(int age){
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}

public static void main(String args[]){
validate(13);
System.out.println("rest of the code...");
}
}

"throws" keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.
Ex:
void method_name() throws exception_class_name{
...
}
Image Created3Down

By: [email protected] On: Mon Nov 11 22:16:44 EST 2013 Question Reputation0 Answer Reputation3 Belt Series Points0 3User Image
Are You Satisfied :4Yes2No
 
1)throw is used to explicitly throw an exception.
throws is used to declare an exception.
2)checked exception can not be propagated without throws.
checked exception can be propagated with throws.
3)throw is followed by an instance.
throws is followed by class.
4)throw is used within the method.
throws is used with the method signature.
5)You cannot throw multiple exception
You can declare multiple exception e.g.
public void method()throws IOException,SQLException.
Image Created3Down

By: [email protected] On: Tue Nov 12 00:29:34 EST 2013 Question Reputation0 Answer Reputation359 Belt Series Points0 359User Image
Are You Satisfied :6Yes2No