Example & Tutorial understanding programming in easy ways.

What is use of throws keyword?

In java the use of throws is, if a code written within a method throws an exception, there can be two cases.

 Either the code is being enclosed by try block and exception handling is implemented in the same method, or the method can throws the exception to the calling method simply.

This is what ‘throws’ does in exception handling, it throws the exception to immediate calling method in the hierarchy. The throws keyword appears at the end of a method's signature. See the example
below:

//Example for demonstrating throws :-

    public void callingMethod() {  
      try {  
       calledMethod();  // here the exception is handled   
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       e.printStackTrace();  
      }  
     }  
       
     public void calledMethod() throws IOException{  
      BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));  
      bfr.readLine(); // because of throws keyword the exception occured here will be handled by calling method  
     } 

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!