Sometimes we explicitly want to create an exception object and then throw it to halt the normal processing of the program.
The throw keyword is used to throw exceptions to the runtime to handle it. It is mainly used to throw a custom exception.
Types of Exceptions in Java
1. User-Defined Exceptions
2. Built-In Exceptions
We can throw either checked or unchecked exceptions in Java by throw keyword.
Build-In Exceptions are of two types:
1) Unchecked Exceptions
2) Checked Exceptions
Unchecked Exceptions
1. ArithmeticException
2. ClassCastException
3. NullPointerException
4. ArrayIndexOutOfBoundsException
5. ArrayStoreException
6. IllegalThreadStateException
Example
Java
package com.prac;public class UncheckException {public static void main(String[] args) {validate(15);}public static void validate(int age) {if (age < 18) {// throw exception if not eligible to votethrow new ArithmeticException("Person is not eligible to vote");} else {System.out.println("Person is eligible to vote!!");}}}
Output:
Exception in thread "main" java.lang.ArithmeticException: Person is not eligible to vote at com.prac.UncheckException.validate(UncheckException.java:11)
Checked Exceptions Throw
1.ClassNotFoundException
2.InterruptedException
3.IOException
4.InstantiationException
5.SQLException
6.FileNotFoundException
Example
Java
package com.prac;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;public class CheckedException {public static void main(String[] args) {readFile();}public static void readFile() {String fileName = "file";File file = new File(fileName);FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}}}
Output
java.io.FileNotFoundException: file (The system cannot find the file specified) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:211) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153) at com.prac.CheckedException.readFile(CheckedException.java:17) at com.prac.CheckedException.main(CheckedException.java:9)
User-defined Exception Throw
Approach
Create a User Defined Exception class
Java
UserDefinedException.java
// class represents user-defined exceptionclass UserDefinedException extends Exception {/****/private static final long serialVersionUID = 1L;public UserDefinedException(String str) {// Calling constructor of parent Exceptionsuper(str);}}
Test User Defined Exceptions
Java
TestThrow.java
public class TestThrow {public static void main(String args[]) {try {// throw an object of user defined exceptionthrow new UserDefinedException("This is user-defined exception");} catch (UserDefinedException ude) {System.out.println("Caught the exception");System.out.println(ude.getMessage());}}}
Output
Caught the exception This is user-defined exception
Exception Handling Using Throws keyword
Java try/ catch block exception handling
Java catches Multiple Exceptions
Exception Handling Using Throw keyword
Java nested try block exception handling
Java custom exception handling
Java finally block exception handling
Exception Handling Interview Questions
No comments:
Post a Comment