Exception and Package
Introduction to ExceptionAn Exception is an abnormal condition that arises in a code sequence at run time, ie. run-time error. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
General for of an exception-handling block:
try{ // block of code to monitor for errors }
catch (ExceptionType1 exOb){// exception handler for ExceptionType1 }
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2 }
//…..
finally{ // block of code to be executed before try block ends }
Mechanism of Exception Handling in Java
Java exception handling is managed via five keywords: try, catch, throw, throws and finally. Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. For any code that throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to a parent class).
Any code that absolutely must be executed after a try block completes is put in a finally block.
Explain the different types of exceptionsThere are three main types of Exceptions:
1. Checked exceptions: which have to be handled by the code. These represent avoidable exceptional conditions which can be handled and recovered from.
2. Runtime Exceptions: which need not be handled by the code. These represent unexpected exceptional conditions which can be handled but not necessarily recover from.
3. Errors: which need not be handled by the code. These represent severe unexpected exceptional conditions which should not be attempted to handle.
There are two Process of Exception Flow
Checked & Unchecked it one of common condition
here Java used Type of Exception Following Types
(1)IOException
(2)FileNotFoundException
(3)ArithmeticException
(4)NullPointerException
(5)ArrayIndexOfBoundException
(6)NegativeArraySizeException
(7)NumberFormatException
What are the different between error and exceptions? Explain about some exceptions in Java. [2064]
Explain about some exceptions in Java. [2064]
Error Exception
1. Error is the compile time error 1. Exception is an run time error
2. Error is uncoverable 2. Exception iscoverable
3. Error can not behandled 3. Exception can be handled
4. For example: Overflow,Out of memory. 4. Examples for exceptions: Array out of bonds, attempt to divide by zero etc.
Basic of exceptionsJava exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java runtime system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block. This is the general form of an exception-handling block:
User-defined exceptionsA user defined exception is exception class provided by API provider or application provider which extends Exception defined in the classes of JAVA API.It's used to deal with the unconventional action in your codes. JAVA use try-catch construct to take it.
In user defined exception class, you must rewrite method extends its parent class Exception. You can throw the exception out in try construct and deal with in catch.
How can you create user define exception?
When a program encounters an exceptional condition, it throws the exception like IOException, IllegalArgumentException etc.
Sometimes no standard class adequately represents the exceptional condition. In this condition programmer can choose to create his own exception class.
To create our own Exception existing type of exception are inherited, preferably one that is close in meaning to your new exception.
This code shows the use of user defined exception. In this code ApplicationException is user defined Exception. public class ApplicationException extends Exception { private int intError;
ApplicationException(int intErrNo){
intError = intErrNo; }
ApplicationException(String strMessage){
super(strMessage); }
public String toString(){
return "ApplicationException["+intError+"]"; }
}
class ExceptionDemo{
static void compute(int a) throws ApplicationException{
System.out.println("called compute(" +a +" )");
if(a>10){
throw new ApplicationException(a);
}
System.out.println("NORMAL EXIT"); }
public static void main(String[] args) {
try{ compute(1); compute(20);
}catch(ApplicationException e){
System.out.println("caught " + e);}} }
Output:
called comute(1 )
mohti
called comute(20 )
caught ApplicationException[20]
Packages:
Packages are collection of similar classes grouped together. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.
Using packages
package java.awt.event;
import java.util.ArrayList; // Import the ArrayList class from the java.util package import java.util.*; // Import all public classes from the java.util package
Introduction to ExceptionAn Exception is an abnormal condition that arises in a code sequence at run time, ie. run-time error. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
General for of an exception-handling block:
try{ // block of code to monitor for errors }
catch (ExceptionType1 exOb){// exception handler for ExceptionType1 }
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2 }
//…..
finally{ // block of code to be executed before try block ends }
Mechanism of Exception Handling in Java
Java exception handling is managed via five keywords: try, catch, throw, throws and finally. Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. For any code that throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to a parent class).
Any code that absolutely must be executed after a try block completes is put in a finally block.
Explain the different types of exceptionsThere are three main types of Exceptions:
1. Checked exceptions: which have to be handled by the code. These represent avoidable exceptional conditions which can be handled and recovered from.
2. Runtime Exceptions: which need not be handled by the code. These represent unexpected exceptional conditions which can be handled but not necessarily recover from.
3. Errors: which need not be handled by the code. These represent severe unexpected exceptional conditions which should not be attempted to handle.
There are two Process of Exception Flow
Checked & Unchecked it one of common condition
here Java used Type of Exception Following Types
(1)IOException
(2)FileNotFoundException
(3)ArithmeticException
(4)NullPointerException
(5)ArrayIndexOfBoundException
(6)NegativeArraySizeException
(7)NumberFormatException
What are the different between error and exceptions? Explain about some exceptions in Java. [2064]
Explain about some exceptions in Java. [2064]
Error Exception
1. Error is the compile time error 1. Exception is an run time error
2. Error is uncoverable 2. Exception iscoverable
3. Error can not behandled 3. Exception can be handled
4. For example: Overflow,Out of memory. 4. Examples for exceptions: Array out of bonds, attempt to divide by zero etc.
Basic of exceptionsJava exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java runtime system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block. This is the general form of an exception-handling block:
User-defined exceptionsA user defined exception is exception class provided by API provider or application provider which extends Exception defined in the classes of JAVA API.It's used to deal with the unconventional action in your codes. JAVA use try-catch construct to take it.
In user defined exception class, you must rewrite method extends its parent class Exception. You can throw the exception out in try construct and deal with in catch.
How can you create user define exception?
When a program encounters an exceptional condition, it throws the exception like IOException, IllegalArgumentException etc.
Sometimes no standard class adequately represents the exceptional condition. In this condition programmer can choose to create his own exception class.
To create our own Exception existing type of exception are inherited, preferably one that is close in meaning to your new exception.
This code shows the use of user defined exception. In this code ApplicationException is user defined Exception. public class ApplicationException extends Exception { private int intError;
ApplicationException(int intErrNo){
intError = intErrNo; }
ApplicationException(String strMessage){
super(strMessage); }
public String toString(){
return "ApplicationException["+intError+"]"; }
}
class ExceptionDemo{
static void compute(int a) throws ApplicationException{
System.out.println("called compute(" +a +" )");
if(a>10){
throw new ApplicationException(a);
}
System.out.println("NORMAL EXIT"); }
public static void main(String[] args) {
try{ compute(1); compute(20);
}catch(ApplicationException e){
System.out.println("caught " + e);}} }
Output:
called comute(1 )
mohti
called comute(20 )
caught ApplicationException[20]
Packages:
Packages are collection of similar classes grouped together. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.
Using packages
package java.awt.event;
import java.util.ArrayList; // Import the ArrayList class from the java.util package import java.util.*; // Import all public classes from the java.util package
What happens if an exception is not caught?
The program will terminate abnormally and then default exception handler will handle the exception.
What are the different ways to handle exceptions?
There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.
The program will terminate abnormally and then default exception handler will handle the exception.
What are the different ways to handle exceptions?
There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.