Before start, let me introduce myself. I am Senior Software Engineer working as full time in an Indian Startup having 2+ YOE. Follow me here for better backend content and connect on linked for better connection.
In Java all exceptions are subclasses of the builtin class Throwable
. In simple word, exception is something that we face during runtime, and it is something that the Java program is not expecting from the code block. It can be because of the polluted input or access of index which doesn’t exist. Let’s create a simple exception.
class Main {
public static void main(String[] args) {
System.out.println("Hello world - 1");
int d = 0;
int a = 41 / d;
System.out.println("Hello world - 2");
}
}
When you run the above program it will throw an exception like
Hello world - 1
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:5)
exit status 1
So, this exception is unhandled and program stop executing after the compiler got this unhandled exception. And this doesn’t print the Hello world — 2
because the program stops there itself.
Let’s handle this simple exception using try catch
block. A simple definition of try and catch block is try consist of our code which is executable and catch handles the exception.
class Main {
public static void main(String[] args) {
System.out.println("Hello world - 1");
try {
int d = 0;
int a = 41 / d;
} catch (ArithmeticException e) {
System.out.println("This is ArithmeticException which is handled gracefullly");
}
System.out.println("Hello world - 2");
}
}
And now the output is
Hello world - 1
This is ArithmeticException which is handled gracefullly
Hello world - 2
Throw Exception using throw
You can throw the exception in Java using the keyword throw
. A simple example of it is
class Main {
static void example() {
try {
throw new NullPointerException("From Example");
} catch (NullPointerException e) {
System.out.println("Exception inside Example");
throw e;
}
}
public static void main(String[] args) {
try {
example();
} catch (NullPointerException e) {
System.out.println("Recaught - " + e);
}
}
}
And the output will be:
Exception inside Example
Recaught - java.lang.NullPointerException: From Example
This is how we are throwing the NullPointerException
Throws
Keyword: It is used when your method is able to throw the exception which is not handled by your method then you need to list the exception that you are throwing, like
type method_name(parameter_list) throws exception_list{}
// example
public void dosomething() throws IOException, AWTException {
// ....
}
class Main {
static void example() throws IllegalAccessException {
try {
throw new IllegalAccessException("From Example");
} catch (IllegalAccessException e) {
System.out.println("Exception inside Example");
throw e;
}
}
public static void main(String[] args) {
try {
example();
} catch (IllegalAccessException e) {
System.out.println("Recaught - " + e);
}
}
}
Output:
Exception inside Example
Recaught - java.lang.IllegalAccessException: From Example
Custom Exception Class
You can create the custom exception class if you need to send special custom exceptions as per your use case.
class MyException extends Exception {
private String cause;
MyException(String c) {
cause = c;
}
}
How to use this exception
class Main {
static void example() throws MyException {
try {
throw new MyException("It is from Example");
} catch (MyException e) {
System.out.println("Exception inside Example");
throw e;
}
}
public static void main(String[] args) {
try {
example();
} catch (MyException e) {
System.out.println("Recaught - " + e);
}
}
}
Output:
Exception inside Example
Recaught - MyException
Chained Exception
Let’s suppose you need to send the chained exception then you can’t send the two exception together then there is a way to send the chained exception using the initCause
and getCause
methods. Sample code for that
class Main {
static void example() {
try {
NullPointerException e = new NullPointerException("NLP");
e.initCause(new ArithmeticException("Arithmetic Cause"));
throw e;
} catch (NullPointerException e) {
System.out.println("Exception inside Example");
throw e;
}
}
public static void main(String[] args) {
try {
example();
} catch (NullPointerException e) {
System.out.println("Recaught - " + e);
System.out.println("Cause is - " + e.getCause());
}
}
}
and the output is
Exception inside Example
Recaught - java.lang.NullPointerException: NLP
Cause is - java.lang.ArithmeticException: Arithmetic Cause