Java Exceptions and Procedures to Handle Exception

Introduction to Java Exceptions:

Hope you are enjoying the best Java tutorials in our  JAVA training series for Beginners. Our previous tutorial explained about Java interfaces and Abstract classes.

In this tutorial, we will learn the following topics:

  • What is an Exception?
  • Why we need an exception in Java?
  • Try, catch and finally block
  • Exception Types
  • Checked Exception, Unchecked Exception, Errors
  • Throw and throws

Here is a Video tutorial on Handling Java Exceptions:

What is an Exception?

An exception is an event which occurs while the program is running and it disrupts the program execution. Because of which the software product will end abruptly.

When this exception occurs, java creates an object with an error message and information about the class. This is the exception object.

Why we need an Exception in Java?

Class ExcepDemo without Exception:

 public class ExcepDemo {
public static void main(String[]
args) {

int i=5;
int j=0;
System.out.println(i/j);

System.out.println("statement after division");
}
} 

Class ExcepDemo without Exception

OUTPUT:

output- Java Class ExcepDemo without Exception

Class ExcepDemo with Exception Handling:

 public class ExcepDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=5;
int j=0;
try {

System.out.println(i/j);
}
catch(ArithmeticException ae) {

System.out.println("wrong input");
}
System.out.println("statement after division");
}

} 

Java Class ExcepDemo with Exception Handling

OUTPUT:

output- Class ExcepDemo with Exception Handling

Try, catch and finally block:

  • An exception is handled by try, catch block.
  • Statements which can lead to exceptions are written inside the try block.
  • When an exception occurs, the statements inside the catch block will be executed. If an exception does not occur, the catch block will not be executed.
  • Irrespective of exception occurred or not occurred, the final block will be executed.

Exception Types

Unchecked Exception:

As Unchecked exception can be avoided by the proper programming (E.g. null pointer Exception, Arithmetic Exception)  will not have checked by the compiler. Unchecked Exception will be thrown at runtime.

Checked Exception:

  • Checked Exception will be checked by the compiler and its mandatory to throw or handle the exception.
  • This Exception is used to separate the error handling code from the regular code.
  • All the checked exceptions are grouped and it is useful in differentiating the problems.

Example: java.io

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class DemoExpChecked {

public static void main(String[] args)
{

try {
File file=new File("input.txt");
 
BufferedReader br = new BufferedReader(new FileReader(file)) ;
String content;
while ((content=br.readLine())!=null)
  {
    System.out.println(content);
  }
  br.close();

}
catch(IOException e) {
e.printStackTrace();
}
}
} 

Checked Java Exception

Errors

An error can occur due to program logical mistakes or any memory related to JVM problem.

Example: Memory out of bound error or stack overflow error.

Throw and Throws

“Throw” keyword is used to throw the exception, whereas the “throws “keyword is used to declare the exception.

Throw Example Program:

 public class Demothrow {
String content ;
public void driving(String c) {
this.content=c;
if (content.isEmpty())
{
throw new NullPointerException("content is empty");
}
else {
System.out.println("content=="+content);
}
}
public static void main(String[] args) {
Demothrow dt=new Demothrow();

dt.driving("");
}
} 

Throw and throws

Throws Example Program:

Throws are used to give information that this method throws this particular exception. When you call that particular method, you need to handle that exception.

 public class DemoThrows {
int i=2,j=0;
public void checkmethod () throws ArithmeticException{
System.out.println(i/j);
}

public static void main(String[]
args) {

DemoThrows d =new DemoThrows();

try {
d.checkmethod();
}
catch (ArithmeticException ae) {

ae.printStackTrace();
}

}
} 

Throws Example

Key points to be noted:

  • Exceptions are abnormal events that are occurred during the program execution and it will affect the execution flow.
  • An error is different from exceptions. Errors Example: Memory out of error.
  • Checked exceptions are checked during compilation and it is mandatory to deal with this checked exception.
  • An unchecked exception occurs during the runtime.

Conclusion

In this tutorial, we learned about Exception, its types, use of ‘throw’ & ‘throws’ along with the procedures to handle an exception in programming.

Our upcoming tutorial will explain all about Strings in Java!!!

PREV Tutorial | NEXT Tutorial