5 Replies Latest reply: May 22, 2013 11:51 AM by rp0428 RSS

    What is the purpose of throws keyword after method definition ?

    bootstrap
      Hi,
      can anyone tell what is the benefit and purpose of applying throws keyword as the end of method definition?
      I did a simple example but I did'nt understood it's use. This example is :
      public static float SquareRoot(int str) throws NumberFormatException{
                
                if( str <0)
                {
                   throw new ArrayIndexOutOfBoundsException();
                }
                float sqrt = (float) Math.sqrt(str);
                
                return sqrt;
           }
      Can anyone help me to understand the use of throws keyword?
        • 1. Re: What is the purpose of throws keyword after method definition ?
          gimbal2
          The trouble is that you're testing with unchecked exceptions. Try throwing an IOException and see what happens then when you don't specify throws.
          • 2. Re: What is the purpose of throws keyword after method definition ?
            bootstrap
            Lets say I have this code:
            public static float SquareRoot(int str) //throws ArrayIndexOutOfBoundsException
                 {
                      int[] x = new int[4];
                      x[0] = 0;
                      x[1] = 1;
                      
                      int y = x[4];
                      
                      float sqrt = (float) Math.sqrt(str);
                      
                      return sqrt;
                 }
            Here I am not getting any difference when "throws ArrayIndexOutOfBoundsException" is commented or not. In both cases I am getting exception. So what is the purpose of placing "throws ArrayIndexOutOfBoundsException" after method name?
            • 3. Re: What is the purpose of throws keyword after method definition ?
              gimbal2
              Yeah sure, completely ignore what I say. Good day.
              • 4. Re: What is the purpose of throws keyword after method definition ?
                aksarben
                Google "Java checked exceptions". Do some reading on the difference between "checked" and "unchecked" exceptions. You'll see that the compiler complains if you don't have a "throws" clause for a method that may throw a "checked" exception.
                • 5. Re: What is the purpose of throws keyword after method definition ?
                  rp0428
                  >
                  can anyone tell what is the benefit and purpose of applying throws keyword as the end of method definition?
                  >
                  Sure - start by reading the relevant sections in the Java Language Specification - it explains ALL of the details about exceptions. Here are two excerpts
                  http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.6
                  >
                  8.4.6. Method Throws
                  A throws clause is used to declare any checked exception classes (§11.1.1) that the statements in a method or constructor body can throw (§11.2.2).
                  . . .
                  11.2. Compile-Time Checking of Exceptions
                  The Java programming language requires that a program contains handlers for checked exceptions which can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception (§11.2.3).

                  This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled. The checked exception classes (§11.1.1) named in the throws clause are part of the contract between the implementor and user of the method or constructor. The throws clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw (§8.4.8.3).
                  >
                  The next step is to read the Exceptions section of the Java Tutorial. It explains how to use exceptions and has example code you can practice with.
                  http://docs.oracle.com/javase/tutorial/essential/exceptions/
                  >
                  Lesson: Exceptions
                  The Java programming language uses exceptions to handle errors and other exceptional events. This lesson describes when and how to use exceptions.

                  What Is an Exception?
                  An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

                  The Catch or Specify Requirement
                  This section covers how to catch and handle exceptions. The discussion includes the try, catch, and finally blocks, as well as chained exceptions and logging.

                  How to Throw Exceptions
                  This section covers the throw statement and the Throwable class and its subclasses.

                  The try-with-resources Statement
                  This section describes the try-with-resources statement, which is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.

                  Unchecked Exceptions — The Controversy
                  This section explains the correct and incorrect use of the unchecked exceptions indicated by subclasses of RuntimeException.

                  Advantages of Exceptions
                  The use of exceptions to manage errors has some advantages over traditional error-management techniques. You'll learn more in this section.

                  Summary
                  Questions and Exercises