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

Java SDK is not so comfortable with people used to command line interpreter. IDEs contain compilers, editors, debugging facilities, drag and drop methods, etc. This compiler strive

Prepare Android Antivirus with locate device capability Project Description: We need a developer that can develop an Android Antivirus similar to lookout Developer need to prov

how to implement brute foce in a program?

The Web container is used for managing the servlet's life cycle. The Web container produces an instance of the servlet and then the container invokes the init() function. At the co

ineed program for dda algorithem

Explain the both HashSet and TreeSet? HashSet is an unordered, unsorted Set. It's a Collection set which restrict duplicate elements and also repositioning of elements. I

Explain continue statement with example? It is sometimes essential to exit from the middle of a loop. Sometimes you'll need to begin over at the top of the loop. Sometimes you'

Explain garbage collection. Java uses garbage collection to free the memory. By cleaning those objects which is no longer reference by any of the program. Step involve in clean

Too many function invocations between the server and the client will lead to network overhead, tight coupling due to dependencies between the server and the client, misuse of serve