Course Content

Course Content

Description

Nested Try and Throw Keyword in Java

In our previous video we understood the concept of Serialization and Deserialization in Java. Now, in this video we are going to see what is Nested Try Block and Throw Keyword in Java programming.

The try block within a try block is known as Nested Try Block in Java.

Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error.
Syntax :
try { statement 1; statement 2;
try { statement 1; statement 2;
}
catch(Exception e) { }
}
catch(Exception e) { }

Example :
class Excep
{
public static void main (String args[])
{
try
{
try {
System.out.println("going to divide by zero");
int b=39/0;
}
catch (ArithmeticException e)
{
System.out.println(e);
}
try {
int a[]=new int [5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("other statement");
}
catch (Exception e)
{
System.out.println("handeled");
}
System.out.println("normal flow..");
}
Output :
going to divide by zero
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 5 other statement
normal flow..

Now we'll understand what is Throw keyword in Java with the help of an Example.
public class thr
{
static void validate(int age)
{
if (age<18)
throw new ArithmeticException ("not valid");
else
System.out.println ("welcome to vote");
}
public static void main (String args[])
{
validate(21); System.out.println ("rest of the code...");
}
}
Output :
welcome to vote
rest of the code...

Hence, we have seen that what is Nested Try Block and Throw Keyword in Java programming with example.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere