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
Overview Demonstrate your knowledge of JavaScript and ColdFusion by implementing the following functionality: Contact form • The following validation error message shou

An RMI "service" could well be any Java method that can be invoked remotely. The other service is the JRMP RMI naming service which is a lookup service.

ActionServlet performs the role of Controller: ? Process user requests ? Verify what the user is trying to achieve according to the request ? Pull data from the model (if

Read information of n students. The class name should be kkhsou with the member variable name, rollno, center_code and percentage in last examestion.Display the students data whose

i need java program for account to hold payment of student

Using Java, write the class Time which consists of the following: (a) three private integer data members (hours, minutes and seconds). (b) a constructor to initialise the dat

Question 1: (a) Describe two reasons driving the termination of processes. (b) Discuss the five main steps the OS would implement once it has decided to create a new proc

hi,i need whatsapp panel to send unlimitd message. skype : maya123.com email :

Create a program Using Constructors? The further program uses the constructor to initialize a car rather than setting the areas directly. class CarTest7 { public static voi