Exceptions in Java

What are the Errors?

Type of Errors

  • Compile time Errors
  • Run time Errors

Compile Time Errors

Run Time Errors

  • Dividing an integer by zero.
  • Converting invalid String to a Number
  • Try to store a value into an array of an incompatible class or type.
  • Accessing an element that is out of the bound of the array.
  • Passing a parameter that is not in a valid range.

Exceptions

  • Find the Problem (Hit the exception)
  • Inform that an error has occurred (Throw the Exception)
  • Receive the error information (Catch the Exception)
  • Take proper actions (Handle it)
  • Checked Exceptions
  • Unchecked Exceptions

Syntax

try{//Statement that causes on Exception
//Here exception Object creates and throws it to catch clause

}catch(ExceptionType e){
//Statement that handles the exception
//Exception handler
}
try{

}catch(ExceptionType e){

}

Multiple Catch Statements

try{

}catch(ExceptionType e){

}catch(ExceptionType e){

}catch(ExceptionType e){

}

Exercise 1

package exceptiontutorial;import java.util.Scanner;/**
*
* @author Kasun Dissanayake
*/
public class ExceptionTutorial {
public static void main(String[] args) {
int[] array = new int[2];
int result = 0;
Scanner scanner = new Scanner(System.in);
try {
array[0] = scanner.nextInt();
array[1] = scanner.nextInt();

result = array[0]/array[1];

} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Wrong index");
} catch (ArithmeticException e) {
System.out.println("Wrong division");
}
System.out.println("Result is : " + result);
}

}

Finally Statement

try {           

} finally{

}
-----------------------------------try {

}catch(){

}finally{

}
package exceptiontutorial;import java.util.Scanner;/**
*
* @author Kasun Dissanayake
*/
public class ExceptionTutorial {
public static void main(String[] args) {
int[] array = new int[2];
int result = 0;
Scanner scanner = new Scanner(System.in);
try {
array[0] = scanner.nextInt();
array[1] = scanner.nextInt();

result = array[0]/array[1];

} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Wrong index");
} catch (ArithmeticException e) {
System.out.println("Wrong division");
}finally{
System.out.println("Result is : " + result);
}
}

}

How to throw our own exceptions?

package exceptiontutorial;import java.util.Scanner;/**
*
* @author Kasun Dissanayake
*/
class MyClass extends Exception{
public MyClass( String x) {
super(x);
}

}
public class ExceptionTutorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c ;

try {
if(a == 0 && b == 0){
throw new MyClass("Both Numbers are zero");
}
} catch (MyClass e) {
System.out.println(e.getMessage());
}
c = a / b;
System.out.println("Result: " +c);

}

}
package exceptiontutorial;/**
*
* @author Kasun Dissanayake
*/
class MyClass {
public void devide(int a, int b) throws ArithmeticException{
int c = a/b;
System.out.println("Answer : "+c);
}

}
public class ExceptionTutorial {
public static void main(String[] args) {
MyClass myClass = new MyClass();
try {
myClass.devide(2, 3); // You can change para here
} catch (ArithmeticException e) {
System.out.println("Wrong division");
}


}

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile