The term exception is shorthand for the phrase "exceptional event."
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
“When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception.”
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
Error - Something severe enough has gone wrong the most applications should crash rather than try to handle the problem,
Unchecked Exception (aka RuntimeException) - Very often a programming error such as a NullPointerException or an illegal argument. Applications can sometimes handle or recover from this Throwable category -- or at least catch it at the Thread's run() method, log the complaint, and continue running.
Checked Exception (aka Everything else) - Applications are expected to be able to catch and meaningfully do something with the rest, such as FileNotFoundException and TimeoutException...
Thrown when an application attempts to use null in a case where an object is required. These include:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
public class NullPointerEx {
public static void main(String[] args){
String[] words = {"Hi", "Hello", "Hola"};
int countNull = 0;
for(int i = 0; i < words.length-1; i++){
if(words[i].equals(words[i+1])){ //reference to !null value
countNull++;
}
}
System.out.println("Null values: " + countNull);
}
}
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the length/size of the array.
public class ArrayIndexOutOfBounds {
public static void main(String[] args){
String[] words = {"Hi", "Hi", null, "Hello","Hello", null, "Hola"};
int countNull = 0;
int countWordsSimilar = 0;
for(int i = 0; i < words.length; i++){
if(words[i] == null){
countNull++;
}else if(words[i].equals(words[i+1])){//more than the lengthh of the array
countWordsSimilar++;
}
}
System.out.println("Null values: " + countNull);
System.out.println("Number of Similar Words: " + countWordsSimilar);
}
}
Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.
public class ArithmeticEx {
public static void main(String[] args){
int[] numbers = {66, 56, 0, 45, 56};
int[] runningNums = new int[numbers.length];
int count = 0, total = 0, num = 0;
for(int i = 0; i < numbers.length; i++){
if(numbers[i]%2 == 0){
count++;
total += numbers[i];
num = total/numbers[i];
}
runningNums[i] = num;
}
for(int i:runningNums){
System.out.print(i + " ");
}
}
}
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);
Thrown to indicate that a method has been passed an illegal or inappropriate argument or inappropriate values of parameters.