Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Information Technology
25 Jan 2019

What are different exceptions in java?

You must be familiar with the try-catch and multiple catch blocks, let us now see few other exceptions in java.

1) The Throws/Throw keyword: If a method does not handle a check exception , the method must declare it using throws keyword. It appears at the end of the method's signature.

You can throw an exception, either a newly initiated one or an exception that you just catch using the throw keyword. Throws is used to postpone the handling of a checked exception and throw is used to explicitly invoke an exception.

2) The Finally block: The finally block is follows by try block or catch block. A finally block executes irrespective of any exception. Using finally block you can execute statements that that you want to execute even it there occurs exception.

3) try-with-resources: Whenever we use resources we have to explicitly close them using finally block. This is a new exception handling mechanism also referred as automatic resource management. This was introduced in java 7 which automatically closes the resources used within try-catch blocks. To use this statement you simply need to declare the resources within the parenthesis as shown below,

try(FileReader fr = new FileReader("file path")) {
// use the resource
} catch () {
// body of catch
4) User defined exception: You can create your own exception in java under certain points.

5)JVM exceptions: These exception are thrown by the JVM. Examples, ClassCastException, ArrayIndexOutOfBoundsException etc.

6)Programmatic exceptions: This exception are thrown by the application or the API programmers.