Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Try Statements in Java

A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception That way, your program won’t crash if the exception occurs.

The try statement has this general form:

try
{
    statements that can throw exceptions
}
catch (exception-type identifier)
{
    statements executed when exception is thrown
}
finally
{
    statements that are executed whether or not
    exceptions occur

The statements that might throw an exception within a try block. Then you catch the exception with a catch block. The finally block is used to provide statements that are executed regardless of whether any exceptions occur.

Here is a simple example:

int a = 5;
int b = 0;            // you know this won’t work
try
{
   int c = a / b;    // but you try it anyway
}
catch (ArithmeticException e)
{
   System.out.println("Can't do that!");
}

In the preceding example, a divide-by-zero exception is thrown when the program attempts to divide a by b. This exception is intercepted by the catch block, which displays an error message on the console.

Here are a few things to note about try statements:

  • You can code more than one catch block. That way, if the statements in the try block might throw more than one type of exception, you can catch each type of exception in a separate catch block.

    In Java 7, you can catch more than one exception in a single catch block. The exceptions are separated with vertical bars, like this:

    try
    {
        // statements that might throw
        // FileNotFoundException 
        // or IOException
    }
    catch (FileNotFoundException | IOException e)
    {
        System.out.println(e.getMessage());
    }
  • A try block is its own self-contained block, separate from the catch block. As a result, any variables you declare in the try block are not visible to the catch block. If you want them to be, declare them immediately before the try statement.

  • The various exception types are defined as classes in various packages of the Java API. If you use an exception class that isn’t defined in the standard java.lang package that’s always available, you need to provide an import statement for the package that defines the exception class. For example:

    import java.io.*;
  • If you want to ignore the exception, you can catch the exception in the catch block that contains no statements, like this:

    try
    {
        // Statements that might throw
        // FileNotFoundException
    }
    catch (FileNotFoundException e)
        {
        }

    This technique is called “swallowing the exception,” and is considered a dangerous programming practice because program errors may go undetected.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.