Home » Java Basics » try/catch/finally

try/catch/finally

Try Catch Java Example

In this example, we will show how to use the try catch Java exception handler. The exception handling in Java is a mechanism to handle runtime errors so that the normal flow of the program can be maintained. 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 ...

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 »