Throw
Java throw is a keyword in java, Which is used to throw an exception explicitly and used with the mostly custom exception.
In java, We have many predefined exceptions like ArithmeticException, ArrayIndexOutOfBoundsException but programmers can create a new exception and throw it explicitly. These exceptions are called user-defined exception. By the help of throw keyword in exception handling we can throw user defined exception in java.
By the help of throw keyword, we can throw both checked and unchecked exception in java. Java throw keyword is mainly used to throw custom exceptions.
Syntax of throw keyword
throw exception;
class ThrowKeywordExample
{
public void check(int age)
{
if(age<18)
{
throw new ArithmeticException("Age is not valid");
}
else
{
System.out.println("Your welcome to vote");
}
}
public static void main(String args[])
{
ThrowKeywordExample t = new ThrowKeywordExample();
t.check(11);
}
}
output : Exception in thread "main" java.lang.ArithmeticException : Age is not valid.
Note : throw keyword is always used within a method body.
In the above example, Exception are occurred in method a() and it is not handled here so it is propagated to method b() and here it not handled, so it is propagated to method c where this exception is handled easily.
Exception can be handled in any method even in main method.
Note : Unchecked Exceptions are forwarded in calling chain by default.
In java, We have many predefined exceptions like ArithmeticException, ArrayIndexOutOfBoundsException but programmers can create a new exception and throw it explicitly. These exceptions are called user-defined exception. By the help of throw keyword in exception handling we can throw user defined exception in java.
By the help of throw keyword, we can throw both checked and unchecked exception in java. Java throw keyword is mainly used to throw custom exceptions.
Syntax of throw keyword
throw exception;
Java throw Keyword Example
This is a very simple example of java throw keyword. In this example we will check the candidate's age is 18 or not, if he will less than 18 we will throw an ArithmeticException otherwise welcome to vote.
class ThrowKeywordExample
{
public void check(int age)
{
if(age<18)
{
throw new ArithmeticException("Age is not valid");
}
else
{
System.out.println("Your welcome to vote");
}
}
public static void main(String args[])
{
ThrowKeywordExample t = new ThrowKeywordExample();
t.check(11);
}
}
output : Exception in thread "main" java.lang.ArithmeticException : Age is not valid.
Note : throw keyword is always used within a method body.
Another throw Example
class AnotherExampleOfThrow
{
static int sum(int num1, int num2)
{
if(num1 == 0)
{
throw new ArithmeticException("parameter first is not valid");
}
else
{
System.out.println("both parameter are correct");
return num1 + num2;
}
}
public static void main(String args[])
{
int result = sum(0,10);
System.out.println(result);
System.out.println("continue next statements");
}
}
output : Exception in thread "main" java.lang.ArithmeticException : parameter first is not valid.
Exception Propagation
In exception propagation, An exception thrown from the top of the stack and if it is not caught, its drops down the call stack to the previous method, and again if not caught there , the exception again drops down to the previous method and so on until they are caught.
Example of Exception Propagation in Java
class ExampleOfEP
{
void a()
{
int i = 30/0;//Exception Occurred
}
void b()
{
a();
}
void c()
{
try
{
b();
}
catch(Exception e)
{
System.out.println("Exception Handled");
System.out.println("Exception Handled");
}
}
public static void main(String args[])
{
ExampleOfEP ee = new ExampleOfEP();
ee.c();
}
}
output : Exception Handled
output : Exception Handled
In the above example, Exception are occurred in method a() and it is not handled here so it is propagated to method b() and here it not handled, so it is propagated to method c where this exception is handled easily.
Exception can be handled in any method even in main method.
Note : Unchecked Exceptions are forwarded in calling chain by default.
By default checked Exception are not forwarded in calling chain
By default checked exceptions are not forwarded in calling chain but we can forward checked exception in calling chain through throws keyword. We will learn throws keyword in later.
This below example, give compilation error because checked exception cannot be forwarded in calling chain.
class Test
{
void a()
{
throw new java.io.IOException("device error");//Checked Exception
}
void b()
{
a();
}
void c()
{
try
{
b();
}
catch(Exception e)
{
System.out.println("Exception Handled");
System.out.println("Exception Handled");
}
}
public static void main(String args[])
{
Test ee = new Test();
ee.c();
}
}
output : compile time error
Read More:
What is Finally in Java?.
Difference Between Final,Finally,Finalize in Java.
Java Exception Handling Interview Questions.
output : compile time error
Read More:
What is Finally in Java?.
Difference Between Final,Finally,Finalize in Java.
Java Exception Handling Interview Questions.
Nice explanation.Thank you..
ReplyDeleteJava Training in Chennai
thanks for sharing this blog,very useful content
ReplyDeleteThanks for sharing this informative blog java training in chennai
ReplyDeleteNice Post. Keep posting, java training
ReplyDeletejava online course