Exception Handling
What is Exception in Java?
Exception is process in which the flow of executing program terminates during run time.
Exception is an abnormal condition.
so in order to handle this abnormal process we are using exception handling mechanism in java.
Types of Handling Exceptions
we can handle exceptions using three methods
1.Try/Catch Block
2.Using Throws
3.Using Throw
Try/Catch:-
The code which may throw exceptions are written in try block.
In Java Try block must be followed by either catch block or finally block.
To handle this exception we are using catch block.
example:-
public static void main(String[] args) {
try
{
int data=60/0;
//may throw exception
}
//handling the exception
catch(ArithmeticException e){
System.out.println(e);
}
System.out.println("rest of the code");
}
}
}
//handling the exception
catch(ArithmeticException e){
System.out.println(e);
}
System.out.println("rest of the code");
}
}
output:-
java.lang.ArithmeticException: / by zerorest of the code
Throws keyword:-
Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException.
import java.io.IOException;
public class ThrowsExample {
public void readFile(String fileName) throws IOException {
if (fileName == null || fileName.isEmpty()) {
throw new IOException("Invalid file name");
}
}
public static void main(String[] args) {
ThrowsExample example = new ThrowsExample();
try {
example.readFile("nonexistentfile.txt");
} catch (IOException e) {
System.err.println("IOException caught: " + e.getMessage());
}
}
}
Throw keyword:-
The throw keyword is used inside function to throw an exceptions explicitly.
example:-
package exceptions;
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class ThrowExample {
public void checkAge(int age) throws CustomException {
if (age < 0 || age > 150) {
throw new CustomException("Invalid age provided");
}
example:-
public class ThrowsExample {
public void readFile(String fileName) throws IOException {
if (fileName == null || fileName.isEmpty()) {
throw new IOException("Invalid file name");
}
}
public static void main(String[] args) {
ThrowsExample example = new ThrowsExample();
try {
example.readFile("nonexistentfile.txt");
} catch (IOException e) {
System.err.println("IOException caught: " + e.getMessage());
}
}
}
Throw keyword:-
The throw keyword is used inside function to throw an exceptions explicitly.
example:-
package exceptions;
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class ThrowExample {
public void checkAge(int age) throws CustomException {
if (age < 0 || age > 150) {
throw new CustomException("Invalid age provided");
}
else {
System.out.println("Age is: " + age);
}
}
public static void main(String[] args) {
ThrowExample example = new ThrowExample();
try {
example.checkAge(170);
}
System.out.println("Age is: " + age);
}
}
public static void main(String[] args) {
ThrowExample example = new ThrowExample();
try {
example.checkAge(170);
}
catch (CustomException e) {
System.err.println("CustomException caught: " + e.getMessage());
}
}
}
output:-
}
}
}
output:-
CustomException caught: Invalid age provided.
Types Exceptions
1.Checked Exceptions
2.Unchecked Exceptions
Checked Exceptions:-
Checked exceptions are also called as compile-time exceptions because these exceptions are checked at compile-time by the compiler.
package exceptions;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("user.txt");
try {
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
Unchecked Exceptions:-
The unchecked exceptions are also called as runtime exceptions.
The compiler will not check these exceptions during compile time.
In simple words, if a program throws an unchecked exception and even if we didn’t handle or declare it, the program would not give a compilation error.
package exceptions;
public class UncheckedExceptionExample {
public static void main(String[] args) {
String text = null;
try {
int length = text.length();
System.out.println("Length of text: " + length);
} catch (NullPointerException e) {
System.out.println("Error: The string is null");
}
}
}
No comments:
Post a Comment