From the course: Oracle Java Foundations

Unlock this course with a free trial

Join today to access over 24,000 courses taught by industry experts.

Handling exceptions

Handling exceptions - Java Tutorial

From the course: Oracle Java Foundations

Handling exceptions

(soft music) - [Joe] Handling exceptions. Java handles unexpected situations using an exception. Something unexpected happens in the program and Java doesn't know what to do. Therefore, it must take some kind of action. What it does is it creates an exception object containing useful information. For example, your code starts to try to read a file, but the file doesn't exist, and therefore it can't read the file, can't open it. That would be an exception. Or your code tries to write to a database but can't connect to the database 'cause maybe it's down. That would be an exception. So when something happens, an unexpected situation happens in the program, then you need to decide what do you want to do with it as the programmer. Exceptions are a very nice, clean way to handle this. So when something unexpected happens, Java will create an exception object and pass or throw that exception object to the code that invoked the problematic method. Now, what that means is if my code is…

Contents