You are on page 1of 2

(/) About (/about)

(/cn/) (/en/)

Welcome (/en/Welcome) / Try and Catch

Previous Tutorial (/en/Inheritance) Next Tutorial (/en/Abstract_Classes)

Try and Catch


Before going into try/catch statements, let's talk about Exceptions. Exceptions are thrown every time an error
occurs. Some examples: ArrayIndexOutOfBounds is thrown if the index that does not exist in an array is accessed
(e.g: Trying to access arr[5], but arr only goes up to arr[4]) ArithmeticError is thrown if an illegal arithmetic operation
is done (e.g: 42/0, division by zero)

There are lots of exceptions that Java can throw (more than the above).

But, how can you handle exceptions, when you're unsure if an error will occur.

That's the purpose of try/catch! This is the syntax for try/catch:

try {
//Code here
} catch (ExceptionHere name) {
//Replace ExceptionHere with your exception and name with the name of your exception.
//Code if exception "ExceptionHere" is thrown.
}

Execute Code

The code after the try block will be attempted to be run. If the exception in the catch statement is thrown during the
code in the try block is run, run the code in the catch block.

You can tell the user that there is a problem, or anything else.

NOTE: You can also use Exception as the exception to catch if any exception is thrown.

Exercise
In this exercise, you will try to catch problematic code. I will create a problematic code block. I want you to wrap the
problematic code in the try block, than make it print "Problem with code detected" if the exception is thrown.

Code WindowNOTE: Use ArrayIndexOutOfBoundsException as exception, or Exception as the exception.Run


Reset Solution

Start Exercise
Output Window Expected Output Show Code Window

(http://www.dmca.com/Protection/Status.aspx?ID=fd56e7e29e1f43ccbe7ce1023cb5781c)
Copyright © LearnJavaOnline.org. Read our Terms of Use (/tos) and Privacy Policy (/privacy)
(http://www.spoj.com/?utm_campaign=permanent&utm_medium=banner&utm_source=learnx)

Previous Tutorial (/en/Inheritance) Next Tutorial (/en/Abstract_Classes)

Code Window Run Reset Solution

Output Window Expected Output Show Code Window

(http://www.dmca.com/Protection/Status.aspx?ID=fd56e7e29e1f43ccbe7ce1023cb5781c)
Copyright © LearnJavaOnline.org. Read our Terms of Use (/tos) and Privacy Policy (/privacy)

You might also like