An exception is a shorthand for an exceptional event. By definition, it is an event that disrupts the normal flow of the program instruction. It is possible to check for every possible error, but in so doing the code can become quickly unintelligible. Exceptions provide an uncumbrous way to check for errors.
When an error occurs an exception is created. An exception is in most of cases an object of a class Exception that inherits from Throwable and contains an error type and contextual information. This object is passed unto the runtime system. The runtime system then goes through the call stack, a.k.a. method invocation stack, to find an exception handler. If it doesn't find an exception handler, the runtime system terminates the program after displaying the call stack and other helpful information. The listing of a call stack may be familiar with people who have been programming in Java for any length of time.
An exception can be either checked or unchecked. A checked exception is an exception checked by the compiler and it's something that has been declared by the methods. One can reasonably except a checked exception to occur. An unchecked exception is normally a standard runtime exception or an error which extends the class RuntimeException or Error. An unchecked exception such as ArrayIndexOutOfBoundsException normally occurs due to an error in program's logic and it cannot be reasonably recovered from at run time.
The Error class defines the base for subclasses that indicate errors in the JVM itself (VirtualMachineError), or JVM's process of linking your program (LinkageError). These are also unchecked.
Although most of existing exception classes should be sufficient, a new subclass of Exception can be created. It should be for an unchecked exception and created for adding useful data, or allow programmer to handle a specific error differently from others.
public class NoSuchTypeException extends Exception {
public final String typeName;
public NoSuchTypeException(String name) {
super("No type named "" + name + "" found");
typeName = name;
}
}
The syntax for throwing an exception is as follows:
throw expression;
Here is an example code snippet.
public void sendValue(String typeName, Object valueToSend)
throws NoSuchTypeException
{
TypeObj thisType = find(typeName);
if (thisType == null)
throw new NoSuchTypeException(typeName);
thisType.sendValue(valueToSend);
}
Since the newly created Exception is an object, it must be created before it can be utilized for throwing an exception.
When an exception is thrown the statement of expression that caused the exception is said to complete abruptly. If the evaluation of a left-operand causes an exception then no part of the right-operand is evaluated, and so on. The next action to occur is either a finally block, or a catch block.
A synchronous exception is an exception that is contingent upon an interrupt made by a particular instruction whereas an asynchronous exception can occur at any time. There can be two reasons for an asynchronous exception: an error in JVM instruction (not caused by your program's instruction) or an explicit use of deprecated Thread.stop method or stopThread methods of the JVM Tool Interface.
This post is still incomplete and will be finalized later.
There are so called EIGHT "primitive" data types in Java. They themselves could be separated into four separate categories as the following:
Boolean and character data types have only one data type each, and there are 4 types of integer data types, and 2 types of floating point.
A boolean data type is expressed as boolean, and it can be either true or false. One would think the object of this type would only take a bit of the memory, but it actually takes up around 4 bytes or so for each object of this data type.
A character type is expressed as char, and it is 16-bit wide, allowing representations of 65536 characters.
An integer type can be expressed as the following:
There are two data types for representing a real value, float and double. According to Sun (now Oracle), it is not recommended to use float or double for precise values. A "wrapper" class such as BigDecimal is recommended for precise values such as currency. A real value can be expressed as the following:
11 months ago by Michael Han
2 years ago by Michael Han
2 years ago by Michael Han
2 years ago by Socrates
2 years ago by Michael Han
2 years ago by John Doe
2 years ago by Michael Han