What is the use of throws keyword, JAVA Programming

What is the use of throws keyword ?

Rather than explicitly catching an exception you can declare in which your method throws the exception. This passes the repsonsibility to handle it to the method in which invokes your method. This is done with the throws keyword. For instance,
public static void copy(InputStream in, OutputStream out)
throws IOException {

byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);

A single method may have the potential to throw more than one kind of exception. In this case the exception clases are just separated through commas. For example,
public BigDecimal divide(BigDecimal value, int roundingMode) throws ArithmeticException, IllegalArgumentException

You can declare in which your method throws runtime exceptions though you do not have to. The major use of this is as documentation for the programmer. It can also be useful in white box testing.


Posted Date: 5/16/2013 2:59:23 AM | Location : United States

Related Discussions:- What is the use of throws keyword, Assignment Help, Ask Question on What is the use of throws keyword, Get Answer, Expert's Help, What is the use of throws keyword Discussions

Write discussion on What is the use of throws keyword
Your posts are moderated
Related Questions

What is difference between Design and System Architecture? System architecture is the conceptual design which defines the structure and/or behavior of a system. Whereas designs

Preventing Applet Based Social Engineering Attacks To help avoid this, Java applet windows are specifically labeled as such along with an ugly bar that says: "Warning: Applet W

write algorithm to perform the following operations on it create,insertion,deletion,for testing overflow and empty conditions.

What do mean by synchronized methods and synchronized statements ?

What are Checked and UnChecked Exception? A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making a

I need to write a program to assign values to the variable age and print the reason as major or minor.

Explain System Colors in java 1.1? In Java 1.1, the java.awt.SystemColor class is a subclass of java.awt.Color that gives color constants in which match native component colors

What is meant through semantic error? It is an error that a developer encounters while a statement is executed but it was not intended through him (the developer). Such errors ar

Explain primitive data types in java? Java's primitive data types are extremely similar to those of C. They involve boolean, byte, short, long, int, float, double, and char. Th