Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Try Block


1. try statement

All program statements which are going to generate an exception are written inside the try block.

If an exception is found in the try block then it is thrown to particular matching catch block for handling purpose.

If any one statement generates the exception in try block then all other remaining statements are skipped and execution is jumped into the matching catch block.

Syntax

try
{
    // statements which are generating an exception.
    ....
    ....
    ....
}

Note : try block should have catch or finally block for handling an exception. We cannot write only try block.

Example

try
{
    int a=25, b=0;
    int c = a/b; // generates an exception as Divided by zero.
    System.out.println("Division is : "+c); // compiler skipes this line and jumps into catch block.
}
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo