Exception Handling:
====================
For example, gmail account, we need emaid and passwrd
if we entered correct account will be opened succusfuly
if enterd wrong email or passwrd it shows error message "please enter valid email or passwrd"
* that messege is genereted with execption,and it is user understandable language.
created by the proggramer
if we are not using exceptions system will be generated some message like"ORAQW12QSH".
user unable to understand system generated messages.
ex:
public class ExceptionEx{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter first number");
int fnum=sc.nextInt();
System.out.println("Enter second number");
int senum=sc.nextInt();
int div=fnum/senum;
System.out.println(div);
System.out.println("remaining 2000 lines of code");
}
}
o/p:::
exeception occure in 30 line but we didnot mension any exceptions, it will throew error
there only stop the execution , remianing lines of code also not executed, is called abnormal
termination
Without exception Handling:
2.system defined messages()
1.abnormal termination (wherever problem occur adn the same line it will stop the execution)
Exception handling is a mechanisam of converting system error messages into user friendly messages.
2. user friendly
1. normal termination
*try, catch,finally, throw,throws
Before going to discuss these keywords we can discuss errors and exceptions.
Errors are two types.
They are
1)compile time error
2)Run time error(exceptions)
eg:System.out.println() ===>(compile time error)
** Compile time errors are occuring because of poor understanding of the language.
**Run time errors are those which are occuring in program when the user inputs invalid data
=>the run time errors must be always converted by the java programmer into user friendly messages using
the concept of Exception Handling.
Types of Exception:
we have 3 types of exceptions
1)checked exceptions
2)unchecked exceptions
3)Errors
Object java API
Throwable
1)Pre defined 2)User defined
1)Asynchronous 2)synchronous
java.lang.Error deals with program run time
deals with the hardware java.lang.Exception java.lang.RuntimeException
and external problems 1) checked 2)Unchecked
(hard disk) (main memory)
*BootStrapMethodError
*InternalAcessError *FilenotFoundException *ArithematicException
*SecurityExceptionError *ClassNotFoundException *NullPointerException
*OutOfMemoryError *IlligalAcessesException *ClassCasteException
*StackoverflowError *UnsupportedOperationException *IndexOutOfBoundException
*UnknownError *InterruptedException *ArrayIndexOutOfBoundException
*VirtualMachienError *StringIndexOutOfBoundException
*IlligalArgumentException
*NumberFormatException
try
{
//error code
}
catch(exception e)
{
//error handlig code
}
finally
{
//closing the connections or files
}
throw eg:
--------
class ThrowEx{
public int pass(int marks){
if(marks<35)
throw new ArithmaticException("you are failed");
}else
{
Syso("you are passed :" +marks);
}
public static void maon(String args[]){
ThrowEx m= new ThrowEx();
m.pass(33);
}
}
how to create user defined exceptions:
--------------------------------------
take your class and your class must be extends to any other parent exception
1.IdNotFoundException extends RuntimeException/Exception
2. your class need to take param constructor
eg:
class MyException extends Exception{
private int age;
public MyException(int age){
this.age= age;
}
public String totring(){
return "you are not eligeble for vote:"+age;
}
}
public class Exception6{
static void validation(int age) throws MyException{
if(age<18)
throw new MyException(age);
else
Syso("you are eligeble for vote");
}
public static void main(String args[]) throws MyException{
Exception6.validation(16);
Syso("rest of the code");
}
}