Let's learn java programming language with easy steps

Tuesday, 13 June 2017

Java Throws

 

Java Throws Keyword

Java Throws

Throws is a keyword in java which is used to declare an exception in our java program. Throws keyword gives the information to the programmer that there may occur an exception so that programmer can take good decision for maintain the normal flow of the program.

Throws keyword make a java programs handler free.

With Throws keyword we can declare only checked exception like SQLException, IOException, etc.


Advantage of Throws Keyword

  • Throws keyword aware the programmer that there may occur exception so it better for the programmer to provide the exception handling code.
  • Throws keyword make program handler free.
  • Throws keyword is also used for forwarding checked exception in calling chain because by default unchecked exception are forwarded in calling chain.

Some Points About Throws Keyword

  1. Throw keyword inform the programmer about an exception.
  2. Throws keyword are used with the method signature(end of the method).
  3. We can declare only checked exception with throws keyword because unchecked exception can be handled by the programmer.

Syntax of Throws Keyword

return_type method_name()throws Exception_Class_Name{
}


Java Throws Example

In this example we will learn how to apply throws keyword with method and how to forward(propagated) checked exception in calling chain.

import java.io.IOException;
class ThrowsExample
{
void a()throws IOException 
{
throw new IOException("device error");//checked Exception
}
void b()throws IOException
{
a();
}
void c()
{
try
{
b();
}
catch(Exception e)
{
System.out.println("Exception is handled");
}
}
public static void main(String args[])
{
ThrowsExample t = new ThrowsExample();
t.c();
}
}

output : Exception is handled

Note : if you calling a method that declares an exception, you must either caught or declare an exception.



Java Throws Example 1

Case1 : In case you handle the exception,  code will execute fine whether exception occurs during the program or not.

import java.io.*;
class A
{
void b()throws IOException
{
throw new IOException("device error");
}
}
class Test
{
public static void main(String args[])
{
try
{
A a = new A();
a.b();
}
catch(Exception e)
{
System.out.println("Exception is handled");
}
}
}

output : Exception is handled

Case2 : (a) In case you declare the exception , if exception doesn't occur, the code will be execute fine.


import java.io.*;
class A
{
void b()throws IOException
{
System.out.println(" welcome");
}
}
class Test
{
public static void main(String args[])throws IOException
{
A a = new A();
a.b();
}

}

output : welcome

(b) if exception occurs


import java.io.*;
class A
{
void b()throws IOException
{
throw new IOException("device error");
}
}
class Test
{
public static void main(String args[])throws IOException
{
A a = new A();
a.b();
}


}

output : Exception in thread "main" java.io.IOException : device error


Difference Between Throw and Throws

There are some difference between throw and throws keyword in java exception handling.

Throw
  • Throw keyword are used to explicitly throw an exception.
  • Throw keyword are used within the method.
  • We cannot propagate or forward checked exception in calling chain through throw keyword. 
Throws
  • Throws keyword are used to declare an exception.
  • Throws keyword are used with the method signature, not within method body  like throw keyword.
  • By using Throws keyword we can easily propagate or forward a checked exception in calling chain.



Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate