Home » Java Basics » try/catch/finally


Try Catch Java Example

In this example, we will show how to use the try-catch Java exception handler. An exception is an error that may occur during the execution of a program and denotes that the normal flow of the program should be interrupted. NullPointerException, IOException, ArithmeticException, ArrayIndexOutOfBoundsException, are some of the others commonly seen exceptions. Those exceptions can be handled by some block ...

Read More »

try/catch/finally InputStream example

This is an example of an InputStream in a try/catch/finally statement. Using try/catch/finally statement to create an InputStream implies that you should: Create an InputStream and initialize it to null. Open a try statement and initialize the InputStream to a FileInputStream, by opening a connection to an actual file. Include the catch statement to catch any IOExceptions thrown while trying ...

Read More »

try with resource example

The NewResource code below: public class NewResource implements AutoCloseable{ String closingMessage; public NewResource(String closingMessage) { this.closingMessage = closingMessage; } public void doSomeWork(String work) throws ExceptionA{ System.out.println(work); throw new ExceptionA("Exception thrown while doing some work"); } public void close() throws ExceptionB{ System.out.println(closingMessage); throw new ExceptionB("Exception thrown while closing"); } public void doSomeWork(NewResource res) throws ExceptionA{ res.doSomeWork("Wow res getting res to do ...

Read More »