answersLogoWhite

0


Best Answer

Throw is used to actually throw the exception, whereas throws is declarative for the method. They are not interchangeable.

public void myMethod(int param) throws MyException

{

if (param < 10)

{

throw new MyException("Too low!);

}

//Blah, Blah, Blah...

} The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.

If a method is throwing an exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.

User Avatar

Wiki User

โˆ™ 2008-12-09 14:28:04
This answer is:
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between throw and throws in Java?
Write your answer...
Submit
Related questions

What is the difference between throw and throws in net?

"throw" is the keyword to raise an exception. "throws" is a Java keyword that indicates a specific method can potentially raise a named exception. There is no analog in VB.Net or C#. Perhaps there is a "throws" keyword in J#?


Throws statement in java?

throw new Throwable(); or throw new Error("Danger will Robinson!"); or throw new NullPointerException(); etc.


What is difference between java 2 and java?

there is no difference between java2 and java. its same. http://javacodespot.blogspot.com/


Difference between Dynamic binding and message passing in java?

difference between inheritaens &amp; pholimorephezem in java


What is the difference between throw throws and throwable in java?

The keyword throw is used to throw user defined exceptions and it requires a single argument(a throwable class object)ex: throw new XYZException("Test");The keyword throws is used in method signatures to declare that, this method could possibly throw an exception.ex: public void test() throws SQLException {}Throwable is an interface that the Exception class implements and an interface that all user defined class would implicitly implement to ensure that they have exception like behavior..The throw keyword is used to explicitly throw an exception.The throws keyword is used to declare what types of exceptions are thrown by a method.The Throwable class is the superclass of all errors and exceptions in Java.Example:// This class is of type Throwableclass MyClass extends Throwable {// The main method is declared to throw a MyClass Throwable objectpublic static void main(String[] args) throws MyClass {// And let's throw a new instance of MyClass to see what happens.throw new MyClass();}}Of course, this code will be pretty useless:Exception in thread "main" MyClassat MyClass.main(MyClass.java:6)Java Result: 1


Can NullPointerException be used with ArithmaticException using throws keyword in java?

No. You cannot throw or catch Null pointer exceptions


What is difference between java 2 and java 5?

They are different versions. Java 5 is newer than Java 2. Think of it like the difference between the Playstation 1 and the Playstation 3.


What keyword are used in java for exception handling?

The important keywords used in Java with respect to Exception Handling are: a. Throw - The "throw" keyword is used to throw exceptions from inside a method b. Throws - The "throws" keyword is used to signify the fact that the code contents within the current method may be throwing an exception and the calling method must handle them appropriately


What is the difference between basic java and advanced java?

few diff is....


Major difference between c and java?

Java is object oriented, C is not...


What is the difference between coffee and java?

Nothing, java is another name for coffee.


What is the difference between connectivity in java?

kamina


Why should throw keyword is used in java?

Use it when you are implementing something that says it throws a certain exception when a certain condition is met.


What is the difference between access specifiers and modifiers in java?

There is no such thing as an access specifier in Java.


What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Write a java program which throws Arithmetic Exception?

If you want the simplest answer possible, then you can simply throw it yourself. throw new ArithmeticException(); If you want a more useful example: int undefinedValue = 1 / 0; This will throw an ArithmeticException.


Difference between java and symbian phone?

don't No.


Difference between recordset and resultset in java?

Rowset


Difference between access specifier and access modifier in java?

There is no such thing as an access specifier in Java.


What is the difference between JAD and JAR?

JAD-Java Application Description JAR-Java archive


When wil you need throws keyword in java?

The throws keyword will be used in method declaration to signify the fact that, some pieces of code inside the method may throw exceptions that are specified in the method signature.


How do you tell the difference between male and female java finch?

A person can tell the difference between a male and female java finch by the birds beaks. The beaks will be shaped differently.


What is the difference between object oriented programming concepts and java?

java is a programming language/platform that embodies object oriented programming concepts. The question of what is the difference is like asking what is the difference between cars and a volvo.


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


How do you throw exception in java?

we do it using the throw keyword.