answersLogoWhite

0

What is throw keyword in java?

Updated: 8/10/2023
User Avatar

Wiki User

11y ago

Best Answer

The throw keyword is used from within a method to "throw" an exception to the calling method. In order to throw an exception, the method signature must indicate that it will throw an exception. For example: public void foo() throws Exception { } When you want to actually throw the exception, you can do it a few different ways: throw new Exception("Exception message"); or from within a catch block ( catch(Exception ex) ): throw ex;

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

public class Example1 {

public static void main(String[] args) {

int i=1,j =0,k;

try

{

k=i/j;

}catch(ArithmeticException ae)

{

System.out.println("Division by zero,illegal operation");

}

System.out.println("Hello World");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The throw keyword is part of Java's exception handling. An exception is when normal processing fails for some reason (e.g., an IOException would occur because a data file could not be read or written). The Java method that encounters this condition would throw an Exception object. There are many exception classes build into Java (subclasses to the Exception class), and you can create your own exceptions classes by extending one of the existing exception classes.

When the exception us thrown (using the throw keyword), the execution of the method terminates without completing, and the exception object is delivered to the calling method. If that method has exception handling (using try, catch, and finally keywords), the program can perform some actions in response to the exception, and processing can continue. If exceptions are not handled, execution will terminate.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The throw keyword is used to throw User Defined Exceptions explicitly.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is throw keyword in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you throw exception in java?

we do it using the throw keyword.


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#?


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


Is check a keyword in java?

No, 'check' is not a keyword in java language.


Is float is a keyword in java?

yes, float is keyword and data type in java


What types of values can be thrown in java?

Only instances of subclasses of throwable can be used in conjunction with the throw keyword. In java all exceptions and errors are subclasses of throwable.


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

No. You cannot throw or catch Null pointer exceptions


What does the verify keyword do in Java?

"verify" is not a Java keyword. I believe the link, in related links, has the complete list of Java keywords.


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 foreign keyword in java?

There is no "foreign" keyword in Java, however, there is a native keyword that declares native methods in a native language, such as C or C++.For full list of keywords in Java see related question.


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.


What is literal in java programming?

Literal in java are L, F, null, true, false These act as keyword(have special meaning in java) but these does'nt comes under the category of Java Keyword.