Exception Handling
------------------
->The error in a program is called 'Bug'.
->Removing the errors is called 'debugging'.
There are 3 types of errors.
1.compile-time errors:-
----------------------
These are the errors in the syntax (or) grammer of the language.These errors are detected by java compiler at the time of compilation.
2.Run-time errors:-
-------------------
These errors happen due to inefficiency of the computer system to execute a statement.
These errors are detected by JVM at run-time.
3.Logical errors:-
-----------------
These are the errors in the logic of the program.They are not detected by java compiler (or) by JVM.
Logical errors are detected by comparing program o/p with manually calculated results.
Exception:-
An Exception is a run-time error.
All exceptions are represented by classes in java.
diagram:-
IIQ)What are the checked exceptions?
The exceptions detected by java compiler at compilation time are called checked exceptions.
IIQ)What are unchecked exceptions?
The exceptions detected by JVM at Run time are called Un checked exceptions.
-->Exception is the super class of all the Exceptions.
-->Throwable is a class that represents all errors and exceptions.
->When an exception occur in a program JVM terminates the program obnormally and entire user data may be lost.SO,the programmer should perform the following 3 tasks.
step 1:The programmer should write statements in a try block to raise the exception.
syn: try{
statements;
}
When there is an exception in try block,jvm will not terminate the program abnormally.It will store exception details in an "Exception stack" and then jumps into "catch" block.
step 2:The programmer should display exception details and any messages to the user in catch block.
syn: catch(Exceptionclass obj){
statements;
}
->We can display the exception details using any one of the following ways:
1.print() (or) println() methods.such as System.out.println(obj);
2.printStackTrace() method of Throwable class,which fetches exception details from the exception stack and display them.
step 3:close all files and databases in finally block.
syn: finally{
Statements;
}
->Finally block is executed even if there is an exception (or) not.
Performing the above tasks is called "Exception Handling".
ex1:-
ex2:- (ArrayIndexOutOfBoundsException example)
Multiple Exceptions:-
-------------------
summary:-
1.Multiple Exceptions can be handled by writing multiple catch blocks.
2.A single try block can be followed by multiple catch blocks.
3.catch block does not exist with out a try block.
4.but a try exist without a catch.
IIQ)What is the difference between Exception and error?
r)Exception:- An exception is an error and it can be handled.
error:- But an error can not be handled.
ex:-
Throws Clause:-
--------------
1.Throws statement is useful to throw an exception out of a method without handling it.
2.'throws Exception' is useful to throw any type of exception without handling it.
ex:-
Throw:-
-----
This statement is useful to throw an exception object and also handle it.
IIQ)What is the difference between throws and throw?
r)Throws statement is useful to throw an exception without handling it.Throw statement is useful to throw an exception object and handle it explicitly.
uses of throw:-
--------------
1.Throw is used in software testing to test whether a java program is handling all the exceptions as claimed by the user.
2.Throw is useful to create user-defined exceptions and handle them.
ex:-
Types of exceptions:-
1.Built-in Exceptions:-The exceptions which are already available in java.
ex:- 1.IOException
2.ArrayIndexOutOfBoundsException
3.StringIndexOutOfBoundsException
4.ArithmeticException
5.FileNotFoundException
6.ClassNotFoundException
7.NoSuchMethodException
8.NullPointerException
9.RuntimeException
10.InterruptedException
11.NumberFormatException
2.User-defined Exceptions:-
The exceptions created by the user's of the language are called user-defined exceptions.
creating user-defined Exceptions:-
-------------------------------
1.create user-exception class as a sub class to Exception class.
ex:- class MyException extends Exception
2.Write a default constructor in user-exception class
ex:-MyException(){}
3.Write a constructor with a string parameter and from there call super class constructor.
ex:-MyException(String str){
super(str);
}
4.To raise the exception throw exception class object out of try block using throw Statement.
ex:- MyException me = new MyException();
throw me;
ex:- How to create our own exception and raise it.
H.W:-
1.create a program that accepts 2 numbers as input from commandline. Your program should throw RuntimeException if exactly two numbers are not given.
2.create a student class with some students names and their marks in maths.Raise your own exception like unreasonable marks if any student gets more than 100 marks in maths.
No comments:
Post a Comment