1Explain the main principle of exceptions and exception hand

1)Explain the main principle of exceptions and exception handling in terms of the potential difference between the point in a program where a problem is first detected and the point where it might be handled

Solution

Exceptions in real life are rare and usually used to something unusual that does not conform to standard rules. For example, Abraham Lincoln was an exception, despite of all hurdles, became 16th president of USA. In computer programming these are like, Trying to acces 11th element of array with size 10, division by 0, accessing a file which is not present, failure of I/O operations.

In java the topmosr class in the hierarchy is java.lang.throwable. Exception employess stack to record the active frames generated by execution of a program.

They are 2 tyoes of exceptions: checked and unchecked exceptions.

Checked exceptions are those for which the compiler checks to see whether they have been handled in your program or not. Unchecked or runtime exception are not checked by the compiler.

Exception will be handled by try catch: Can be placed within any method that you feel can throw exceptions. All the statements ti be tried for exceptions are put in a try block and immediatley following the try is the catch block. catch block is used to catch any exception raised from the try block. If exception occers in any statement in the try block, the following statements are not executed and the control will be immediately paased ti the respective catch block. A single try can have multiple catch block for catching respective exceptions. They will be a default exception case called Exception. Where if we does not provide any particular exception, the defalut one is invoked.

Example:

class ExDemo{

public static void main(String args[]){method()}

static void method(){

System.out.println(\"In Method, Calling from Main\");

try{

method1();}

catch(Exception e){

System.out.println(\"Exception Handled\");}

System.out.println(\"Returned from method1\");

}

static void method1(){

System.out.println(\"In Method1\");

int a=20, b=0;

int c=a/b;

System.out.println(\"Method2 Exits\");

}

}

Output:

In Method, Calling from Main

In Method1

Exception Handled

Returned from method1

Checked Unchecked
ClassNotFoundException ArthemeticException
NoSuchFliedException ArrayIndexOutOfBoundsException
IOException NullPointerException
IilegalAccessException BufferOverflowException
1)Explain the main principle of exceptions and exception handling in terms of the potential difference between the point in a program where a problem is first d
1)Explain the main principle of exceptions and exception handling in terms of the potential difference between the point in a program where a problem is first d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site