root

personal (4)
cycling (4)
japan (1)
Nippon; in E Asia; capital Tokyo; area 145,882 sq. mi., pop. 123,778,000; Japanese; Shinto and Buddhist; yen
korea, south (5)
Republic of Korea; in NE Asia; capital Seoul; area 38,023 sq. mi., pop. 43,919,000; Korean; Buddhist, Confucian, and Christian; won
literature (1)
natural law (1)
Platonism (1)
CSS 3 (1)
social networking (1)
java (2)


Latest comments:
11 months ago by Michael Han
3 years ago by Michael Han
3 years ago by Michael Han
3 years ago by Socrates
3 years ago by Michael Han
3 years ago by John Doe
3 years ago by Michael Han

wiki

Exceptions in Java

, | 0 comments

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.


Exception types


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;
}
}


throwing an exception


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.


no comments

Add a new comment.
All comments are NOT moderated as of today.

(optional)
(optional)

Please enter the captcha value carefully.
You can lose your comment if you enter a wrong captcha value.
captcha image