Java Tutorial 10
Java Tutorial 10
Java Tutorial 10
(/cn/) (/en/)
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.
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.
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)
(http://www.dmca.com/Protection/Status.aspx?ID=fd56e7e29e1f43ccbe7ce1023cb5781c)
Copyright © LearnJavaOnline.org. Read our Terms of Use (/tos) and Privacy Policy (/privacy)