KEMBAR78
Exception handling in Java | PPTX
Exceptions  Mr.PrasadSawantDepartment Of Computer ScienceProf .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707 Email:sawanprasad@gmail.comBlog:http://prasadmsawant.blogspot.com/
Agenda 	Define exception.Discuss what is meant by exception handing.Describe the try,catch and finally blocks.Examine multiple catch blocks.Explore nested try catch blocks.Explain  the use of throw and throws keywords.Create user defined exception.
What is exception?When an error is encountered during the execution of a program an exception  is said to have occurred.
Handling Exception…IF  B IS ZERO GO TO ERORC=A/BPRINT CGO TO EXIT 		ERROR:		DISPLAY “CODE CAUSING ERROR DUE TO  DIVISION BY   	ZERO”EXIT:ENDBLOCK THAT HANDLES ERROR
HANDLING EXCEPTION IN JAVA OBJECTErrorTHROWABLEExceptionSQLExceptionAWT ERRORClassNotFoundExceptionThread Death..………………
System-Defined ExceptionRaised implicitly by system because of illegal execution of program When cannot continue program execution any more Created by Java System automatically Exception extended from Error class and RuntimeException class   [DivByZero.java] IndexOutOfBoundsException: When beyond the bound of index in the object which use index, such as array, string, and vector ArrayStoreException : When assign object of incorrect type to element of array NegativeArraySizeException : When using a negative size of array NullPointerException :When refer to object as a null pointerSecurityException : When violate security. Caused by security managerIllegalMonitorStateException : When the thread which is not owner of monitor involves wait or notify method
Programmer-Defined Exception Exceptions raised by programmer Check by compiler whether the exception handler for exception occurred exists or notIf there is no handler, it is error Sub class of Exception class
The exception handling modeltry { 	// code  which is expected to throw an exception }Catch (exception e1){	/*	If exception is thrown in the try block ,is of type e1,then perform necessary action here 	*/}
Multiple catch blockClass catch22{   public static void main(String  args[])   {	 try      {		String  num=args[0];intnumValue=Interger.parseInt(num);System.out.println(“The squre is ”+numValue*numvalue”);       }	 catch(ArrayIndexOutOfBoundException  e1)	{System.out.println(“No argument is given!”);	}	catch(NumberFormatException  e2)	      { System.out.println(“Not  a number “);      }     }}C:>javac catch22      No argument is given!C:>javac catch22 aNot  a numberC:>javac  catch22 3The squre is 9
Nested try-catch …try{int num=args.length;  try   {intnumValue=Integer.parseInt(args[0]);System.out.println(“The squre is “+numValue*numValue);  }   catch(NumberFormatExcption e1)  {System.out.println(“Not a Number!”);  }}catch (ArrayIndexOutOfBoundException e2){System.out.println(“No arguments given!”);}….C:>javac catch       No argument is given!C:>javac catch22 aNot  a numberC:>javac  catch22 3The squre is 9
Finally blockThe ‘finally ’block is guaranteed  to run  whether or not an exception occurs. The finally clauses of a try catch block will always execute ,even  if there are  return statement in the try catch part . Try blockNo  exceptionExceptionfinallyCatch  blockfinally
Using throws and throwException  are thrown with the help of the  ‘throw’ keyword  .The ‘throw’ keyword is used  to indicate that an exception has occurred .The operand of throw is an object of any class that is derived from the class ’Throwable’.The  throws keyword is used to list the exception that method can throw.
Exception handling in Java

Exception handling in Java

  • 1.
    Exceptions Mr.PrasadSawantDepartmentOf Computer ScienceProf .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707 Email:sawanprasad@gmail.comBlog:http://prasadmsawant.blogspot.com/
  • 2.
    Agenda Define exception.Discusswhat is meant by exception handing.Describe the try,catch and finally blocks.Examine multiple catch blocks.Explore nested try catch blocks.Explain the use of throw and throws keywords.Create user defined exception.
  • 3.
    What is exception?Whenan error is encountered during the execution of a program an exception is said to have occurred.
  • 4.
    Handling Exception…IF B IS ZERO GO TO ERORC=A/BPRINT CGO TO EXIT ERROR: DISPLAY “CODE CAUSING ERROR DUE TO DIVISION BY ZERO”EXIT:ENDBLOCK THAT HANDLES ERROR
  • 5.
    HANDLING EXCEPTION INJAVA OBJECTErrorTHROWABLEExceptionSQLExceptionAWT ERRORClassNotFoundExceptionThread Death..………………
  • 6.
    System-Defined ExceptionRaised implicitlyby system because of illegal execution of program When cannot continue program execution any more Created by Java System automatically Exception extended from Error class and RuntimeException class [DivByZero.java] IndexOutOfBoundsException: When beyond the bound of index in the object which use index, such as array, string, and vector ArrayStoreException : When assign object of incorrect type to element of array NegativeArraySizeException : When using a negative size of array NullPointerException :When refer to object as a null pointerSecurityException : When violate security. Caused by security managerIllegalMonitorStateException : When the thread which is not owner of monitor involves wait or notify method
  • 7.
    Programmer-Defined Exception Exceptionsraised by programmer Check by compiler whether the exception handler for exception occurred exists or notIf there is no handler, it is error Sub class of Exception class
  • 8.
    The exception handlingmodeltry { // code which is expected to throw an exception }Catch (exception e1){ /* If exception is thrown in the try block ,is of type e1,then perform necessary action here */}
  • 9.
    Multiple catch blockClasscatch22{ public static void main(String args[]) { try { String num=args[0];intnumValue=Interger.parseInt(num);System.out.println(“The squre is ”+numValue*numvalue”); } catch(ArrayIndexOutOfBoundException e1) {System.out.println(“No argument is given!”); } catch(NumberFormatException e2) { System.out.println(“Not a number “); } }}C:>javac catch22 No argument is given!C:>javac catch22 aNot a numberC:>javac catch22 3The squre is 9
  • 10.
    Nested try-catch …try{intnum=args.length; try {intnumValue=Integer.parseInt(args[0]);System.out.println(“The squre is “+numValue*numValue); } catch(NumberFormatExcption e1) {System.out.println(“Not a Number!”); }}catch (ArrayIndexOutOfBoundException e2){System.out.println(“No arguments given!”);}….C:>javac catch No argument is given!C:>javac catch22 aNot a numberC:>javac catch22 3The squre is 9
  • 11.
    Finally blockThe ‘finally’block is guaranteed to run whether or not an exception occurs. The finally clauses of a try catch block will always execute ,even if there are return statement in the try catch part . Try blockNo exceptionExceptionfinallyCatch blockfinally
  • 12.
    Using throws andthrowException are thrown with the help of the ‘throw’ keyword .The ‘throw’ keyword is used to indicate that an exception has occurred .The operand of throw is an object of any class that is derived from the class ’Throwable’.The throws keyword is used to list the exception that method can throw.