Exception Handling is also known as Defensive Programming.
Exception is an indication of a problem that occurs during a program’s execution.
Exception handling enables you to create applications that can resolve (or handle) exceptions. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered. The features help you write robust and fault-tolerant programs that can deal with problems and continue executing or terminate gracefully.
Java exception handling is based in part on the work of Andrew Koenig and Bjarne Stroustrup.
When to Use Exception Handling?
Exception handling is designed to process synchronous errors, which occur when a statement
executes. Common examples we’ll see are out-of-range array indices, arithmetic overflow (i.e., a value outside the representable range of values), division by zero, invalid method parameters, thread interruption
and unsuccessful memory allocation (due to lack of memory).
Exception handling is not designed to process problems associated with asynchronous events (e.g., disk I/O completions,network message arrivals, mouse clicks and keystrokes), which occur in parallel with,
and independent of, the program’s flow of control.
Hierarchy of Exception
An small portion of the inheritance hierarchy for class Throwable (a subclass of Object), which is the superclass of class Exception. Only Throwable objects can be used with the exception-handling mechanism. Class Throwable has two subclasses: Exception and Error.
Class Exception and its subclasses—for instance, RuntimeException (package java.lang) and IOException (package java.io)—represent exceptional situations that can occur in a Java program and that can be caught by the application. Class Error and its subclasses represent abnormal situations that happen in the JVM.
Types of Exception
Java compiler enforces a code written with in try/ catch block or throw an Exception object for checked exceptions.All exception types that are direct or indirect sub classes of class RuntimeException (package java.lang) are unchecked exceptions like ArrayIndexOutOfBoundsExceptions and ArithmeticExceptions.
And other Exception known as checked Exception that are not subclass of RunTimeException class.
You must write Java code for checked exception within try catch block or use throw clause.
Here is an Example through Java Code
public class ExceptionExample{
public static void main(String []args){
try{
// Open File and read data
}
catch(Exception e){
//TO do for to handle Exception
}
}
}
OR throws example are
public class ExceptionExample{
public static void main(String []args) throws Exception{
// Open File and read data
}
}
In above throws example we don't need to write code with in try/catch block.
Hope you got the idea about the Exception handling.I know there are many other concept and techniques related Exception Handling not mentioned in here but reason for that I put consideration in my mind for all audience newbie, Beginner ,Intermediate or Expert.
Exception is an indication of a problem that occurs during a program’s execution.
Exception handling enables you to create applications that can resolve (or handle) exceptions. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered. The features help you write robust and fault-tolerant programs that can deal with problems and continue executing or terminate gracefully.
Java exception handling is based in part on the work of Andrew Koenig and Bjarne Stroustrup.
When to Use Exception Handling?
Exception handling is designed to process synchronous errors, which occur when a statement
executes. Common examples we’ll see are out-of-range array indices, arithmetic overflow (i.e., a value outside the representable range of values), division by zero, invalid method parameters, thread interruption
and unsuccessful memory allocation (due to lack of memory).
Exception handling is not designed to process problems associated with asynchronous events (e.g., disk I/O completions,network message arrivals, mouse clicks and keystrokes), which occur in parallel with,
and independent of, the program’s flow of control.
Hierarchy of Exception
An small portion of the inheritance hierarchy for class Throwable (a subclass of Object), which is the superclass of class Exception. Only Throwable objects can be used with the exception-handling mechanism. Class Throwable has two subclasses: Exception and Error.
Class Exception and its subclasses—for instance, RuntimeException (package java.lang) and IOException (package java.io)—represent exceptional situations that can occur in a Java program and that can be caught by the application. Class Error and its subclasses represent abnormal situations that happen in the JVM.
Types of Exception
- Checked Exception
- UnChecked Exception
Java compiler enforces a code written with in try/ catch block or throw an Exception object for checked exceptions.All exception types that are direct or indirect sub classes of class RuntimeException (package java.lang) are unchecked exceptions like ArrayIndexOutOfBoundsExceptions and ArithmeticExceptions.
And other Exception known as checked Exception that are not subclass of RunTimeException class.
You must write Java code for checked exception within try catch block or use throw clause.
Here is an Example through Java Code
public class ExceptionExample{
public static void main(String []args){
try{
// Open File and read data
}
catch(Exception e){
//TO do for to handle Exception
}
}
}
OR throws example are
public class ExceptionExample{
public static void main(String []args) throws Exception{
// Open File and read data
}
}
In above throws example we don't need to write code with in try/catch block.
Hope you got the idea about the Exception handling.I know there are many other concept and techniques related Exception Handling not mentioned in here but reason for that I put consideration in my mind for all audience newbie, Beginner ,Intermediate or Expert.
No comments:
Post a Comment