Exceptions
What is Exception Handling?• Exception is the one that stops the executionof the program unexpectedly.• The process of han...
Exception Handling MechanismException can be handled in 3 ways:• try block• Catch block• Finally block
Exception ClassesThrowableErrorExceptionRuntimeExceptionsCompile – enforcedExceptionObject12
Try and Catch blocktry{//code where you think exception would occur}catch(Exception_Class reference){//Catch the exception...
Try – Catch examplepublic class Try_Catch {public static void main(String[] args) {int y=0;try {System.out.println(5/y);}c...
Multiple Catches• When there is a chanceof getting differenttypes of exceptions weuse multiple catchblock for a try block....
Multiple- Catch Examplepackage com.edureka.exception.multiplecatch;class Multiple_Catch {int n;int array[]=new int[3];Mult...
Multiple- Catch Examplepackage com.edureka.exception.multiplecatch;class Main {public static void main(String[] args){Mult...
What is throw keyword?• throw is a keyword which is used to call the sub class of anexception class.• This keyword is also...
Example using throw keywordpackage com.edureka.exception.throwkeyword;public class Student {Student(int studentid, String ...
What is throws keyword?• throws is a keyword applied to methods forwhich an exception has raised during itsexecution.retur...
Example using throws keywordpackage com.edureka.throwskeyword;public class GiveInput {void takeInput() throws IOException{...
Uses of finally keyword• When we want a set of statements to be executedeven after an exception has occurred then we usefi...
Types of Exception• Run-time Exceptions.• Compile Enforced Exception
Run-Time Exceptions• Are also called as Unchecked Exception.• These exceptions are handled at run-time i.e by JVMafter the...
Complier-enforced Exceptions• Are also called as Checked Exceptions.• These exceptions are handled by java complierbefore ...
User-defined Exceptions• Across built-in exceptions user can alsodefine his own exceptions.• It can be done by defining a ...
Advantages of Exception• The program will still execute even if an exceptionarises i.e finally block.• If you cant handle ...
Errors and Error Handling Design-time error: These are the errors that occur whiledesigning the programs.Eg: Syntax error...
Errors and Error Handling Logical error: These are the errors done by programmer. Theprograms with these errors will run ...
•Q& A..?
Thanks..!
Upcoming SlideShare
Loading in …5
×

Java class 7

731 views
574 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
731
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
98
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java class 7

  1. 1. Exceptions
  2. 2. What is Exception Handling?• Exception is the one that stops the executionof the program unexpectedly.• The process of handling these exceptions iscalled Exception Handling.
  3. 3. Exception Handling MechanismException can be handled in 3 ways:• try block• Catch block• Finally block
  4. 4. Exception ClassesThrowableErrorExceptionRuntimeExceptionsCompile – enforcedExceptionObject12
  5. 5. Try and Catch blocktry{//code where you think exception would occur}catch(Exception_Class reference){//Catch the exception and displays that exception}
  6. 6. Try – Catch examplepublic class Try_Catch {public static void main(String[] args) {int y=0;try {System.out.println(5/y);}catch(Exception e) {System.out.println(“Divide By Zero Exception”);}}}
  7. 7. Multiple Catches• When there is a chanceof getting differenttypes of exceptions weuse multiple catchblock for a try block.try{//statements}catch(Exception_Class reference){//statements for one type of exception}catch(Exception_Class reference){//statements for other type of exception}
  8. 8. Multiple- Catch Examplepackage com.edureka.exception.multiplecatch;class Multiple_Catch {int n;int array[]=new int[3];Multiple_Catch(int n){try{if(n==0)System.out.println(5/n);else{array[3]=n;System.out.println(array);}}catch(ArrayIndexOutOfBoundsExceptionarrayexception){System.out.println(arrayexception);}catch(ArithmeticException divideexception){System.out.println(divideexception);}}}
  9. 9. Multiple- Catch Examplepackage com.edureka.exception.multiplecatch;class Main {public static void main(String[] args){Multiple_Catch multiplecatch1= new Multiple_Catch(0);Multiple_Catch multiplecatch2= new Multiple_Catch(5);}}
  10. 10. What is throw keyword?• throw is a keyword which is used to call the sub class of anexception class.• This keyword is also used to throw the exception occurred in tryblock to catch block.try{throw new Exception_class(“message”);}catch(Exception_class reference){//statements}
  11. 11. Example using throw keywordpackage com.edureka.exception.throwkeyword;public class Student {Student(int studentid, String name){try{if(studentid==0)throw new Exception("id can not be zero");elseSystem.out.println("The id of "+name+"is:"+studentid);}catch (Exception e) {System.out.println(e);}}}package com.edureka.exception.throwkeyword;public class Main {public static void main(String[] args) {Student student1 = new Student(0,"STUDENT1");Student student2 = new Student(1,"STUDENT2");}}
  12. 12. What is throws keyword?• throws is a keyword applied to methods forwhich an exception has raised during itsexecution.returntype method_name throws Exception_Class{// statements}
  13. 13. Example using throws keywordpackage com.edureka.throwskeyword;public class GiveInput {void takeInput() throws IOException{BufferedReader reader=newBufferedReader(newInputStreamReader(System.in));System.out.println("Enter your name");String name=reader.readLine();System.out.println("Your name is: "+name);}}package com.edureka.throwskeyword;public class Main {public static void main(String[] args) throwsIOException {GiveInput input=new GiveInput();input.takeInput();}}
  14. 14. Uses of finally keyword• When we want a set of statements to be executedeven after an exception has occurred then we usefinally block.• finally{//statements that needs to be executed afterexception}
  15. 15. Types of Exception• Run-time Exceptions.• Compile Enforced Exception
  16. 16. Run-Time Exceptions• Are also called as Unchecked Exception.• These exceptions are handled at run-time i.e by JVMafter they have occurred by using try and catchblock.• Eg: ArrayIndexOutOfBoundsException,ArithmeticExceptionNullPointerException
  17. 17. Complier-enforced Exceptions• Are also called as Checked Exceptions.• These exceptions are handled by java complierbefore they occur by using throws keyword.• Eg: IOException,FileNotFoundException
  18. 18. User-defined Exceptions• Across built-in exceptions user can alsodefine his own exceptions.• It can be done by defining a class that extendsException class and creating a constructor ofthe class (user-defined) with string argumentto print that message when exception occurs.
  19. 19. Advantages of Exception• The program will still execute even if an exceptionarises i.e finally block.• If you cant handle the exception JVM will handle theexception if we use throws keyword.• We can differentiate the exceptions that haveoccurred.
  20. 20. Errors and Error Handling Design-time error: These are the errors that occur whiledesigning the programs.Eg: Syntax errorsThese errors will be shown with a red mark in eclipse IDE sothat you can easily find and correct it.
  21. 21. Errors and Error Handling Logical error: These are the errors done by programmer. Theprograms with these errors will run but does not producedesired results.Eg: getting division of two numbers as output but expected ismultiplication of numbers.These errors can be rectified by understanding the logic andchecking whether it is works out correctly or not.
  22. 22. •Q& A..?
  23. 23. Thanks..!

×