
An exception is an event that disrupts the normal flow of the program. They
are mainly two types:- Compile time and Runtime Exceptions. Compile time
exceptions are checked at compile-time by the compiler. Runtime exceptions are
thrown at runtime. Runtime errors occur while a program is running if the JVM
detects an operation that is impossible to carry out. For example, if you
access an array using an index that is out of bounds, you will get a runtime
error with an
ArrayIndexOutOfBoundsException. If you enter a string value when your program expects an integer, you will
get a runtime error with an
InputMismatchException.
1
2
3
4
5
6
7
8
9
10
11
|
import java.util.Scanner;
public class Exception {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number");
int num1= input.nextInt();
}
}
|
Run the above code and enter a double value or string value as
input:-
You can see an Exception has thrown due to the input mismatch. The Exception
is pointed to a class called
InputMismatchException. Like this there are several built in Exceptions in
java.
When an exception is thrown, the normal execution flow is interrupted. As the
name suggests, to “throw an exception” is to pass the exception from one place
to another. The statement for invoking the method is contained in a try block.
The try block contains the code that is executed in normal
circumstances. The exception is caught by the catch block.The code in the
catch block is executed to handle the exception. Afterward, the statement
after the catch block is executed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | import java.util.InputMismatchException;
import java.util.Scanner;
public class Exception {
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number");
int num1 = input.nextInt();
}
catch (InputMismatchException e){
System.out.println("Enter an integer number");
}
}
}
|
In the above code the exception is handled by a catch block (line no.13). InputMismatchException is a builtin class which don't need to be thrown by the user.
Using "throw" keyword:-
The keyword throw is used to throw an exception. Later this exception is caught by the catch block. You can throw custom exception with the throw keyword. eg: throw new NotNumberException
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | import java.util.Scanner;
public class QuotientWithException {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter first number: ");
int num1 = input.nextInt();
System.out.println("Enter first number: ");
int num2 = input.nextInt();
try {
if(num2==0){
throw new ArithmeticException("Division by zero");
}
double result = num1/num2;
System.out.println("Result is "+result);
}
catch (ArithmeticException e){
System.out.println(e+" exiting...");
}
}
}
|
On the line 13, an if condition is used to throw an Exception explicitly. Thereby we can add our message with the builtin one.
Output:-
"throw and throws"
Throw is used to throw an exception explicitly when throws is used as a method signature.
Throws is used to declare an exception which might get thrown by the method. Throws act as a warning which describes, the method may throw some exception.