1 / 23

Exceptions Handling

Tutorial. Exceptions Handling. www.btechsmartclass.com. Introduction.

aracely
Download Presentation

Exceptions Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial Exceptions Handling www.btechsmartclass.com

  2. Introduction In any programming there will be some problems like, trying to access a resource which is not available, giving wrong input values, hardware problems like network connection etc., these kind of problems will not be known at the time of program compilation. The compiler checks whether the program is as per the syntax rules of that programming language…. These problems will came to know, when we try to execute(run) that program…. So we call them as “Runtime Errors” In such a kind of situation, What the Runtime machine does? Simply, Stops program execution and causes abnormal termination. This kind of situation is known as “EXCEPTION”

  3. Definition Exception is an abnormal condition Dictionary Meaning: An exception is a problem that arises during the execution of a program An exception handling is the concept which is used to handle exception

  4. Concept In java, Exception is an object that describes the exception that occurred in a program. Whenever exception occurs in a program, it creates an object of respective exception class and sends it to a block of code which handles that situation….. So that the program execution will not be terminated abruptly.

  5. Concept Exception handling will not corrects the runtime error, but just informs the user by providing some information about that error.

  6. Reasons An exception can occur for many different reasons, including the following…. A user has entered invalid data A file that needs to be opened cannot be found A network connection has been lost in the middle of communications or the JVM has run out of memory Physical problem like device not working, cable related problem

  7. Handling Exceptions To handle an exception in java we use the following keywords… 1 try 2 catch 3 finally 4 throw 5 throws

  8. Handling Exceptions To handle an exception in java we use the following keywords… 1 try 2 catch The try keyword is used to define a block of statements which may generate exception 3 finally 4 throw 5 throws

  9. Handling Exceptions To handle an exception in java we use the following keywords… 2 catch 1 try The catch keyword is used to define a block of statements which can handle the exception occurred in try block 3 finally Every try block must have atleast one catch block 4 throw The try block may have multiple catch blocks 5 throws

  10. Handling Exceptions To handle an exception in java we use the following keywords… 3 finally 1 try The finally keyword is used to a block of statements which must be execute irrespective of Exception occurance. 2 catch 4 throw 5 throws

  11. Handling Exceptions To handle an exception in java we use the following keywords… 4 throw 1 try The throw keyword is used to throw an exception explicitly. 2 catch 3 finally 5 throws

  12. throw Keyword import java.io.*; classThrowDemo{ publicstaticvoid main(String args[]) { Scanner input = new Scanner(System.in); System.out.println(“Enter any two Integer values”); int a = input.nextInt(); int b = input.nextInt(); try{ if(b == 0) throw new Exception(“Can not divide!!!”); int result = a / b; System.out.println(“Result of a / b = ”+result); } catch(Exception e){ System.out.println(e); } } } Example

  13. Handling Exceptions To handle an exception in java we use the following keywords… 5 throws 1 try The throws keyword is used to list the types of Exceptions that a method might throw. 2 catch 3 finally 4 throw

  14. throws Keyword classThrowsDemo { void myMethod(int n) throws IOException,ClassNotFoundException { if(n ==1) throw new IOException(“Message 1 !!!”); else throw new ClassNotFoundException(“Message 2 !!!”); } } classThrowsDemoTest { publicstaticvoid main(String args[]) { ThrowsDemo obj = new ThrowsDemo(); try{ obj.myMethod(1); } catch(Exception e){ System.out.println(e); } } } Example

  15. Categories In java there are TWO types of exceptions Checked Exceptions The checked exceptions are checked at compile-time Unchecked Exceptions The unchecked exceptions are checked at runtime

  16. Checked Exceptions Checked exceptions should handle the exception using  try - catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error. IOException SQLException DataAccessException ClassNotFoundException

  17. Checked Exceptions Example import java.io.*; classExample { publicstaticvoid main(String args[]) { FileInputStream fis = newFileInputStream("B:/myfile.txt"); /*This constructor FileInputStream(File filename) throws FileNotFoundException */ int k; while(( k = fis.read() ) != -1) { System.out.print((char)k); } fis.close(); /*The method close() closes the file input stream * It throws IOException*/ } }

  18. Unchecked Exceptions Unchecked Exceptions mostly arise due to programming errors like accessing method of a null object, accessing element outside an array bonding or invoking method with illegal arguments etc,. NullPointerException ArrayIndexOutOfBound IllegalArgumentException IllegalStateException

  19. Exception Hierarchy In java all the Exceptions are defined as Classes to handle them. All those classes are in following hierarchy… Throwable Error Exception RunTimeException IOException SQLException NullPointerException IndexOtOfBoundsException NumberFormatException …..

  20. Exception Hierarchy

  21. Exception Hierarchy

  22. Creating Exceptions To create your own exception types to handle situations just define a subclass of Exception

  23. classMyOwnExceptionextendsException { publicMyOwnException(String msg){ super(msg); } } classEmployeeTest { staticvoid employeeAge(int age) throwsMyOwnException{ if(age < 0) thrownewMyOwnException("Age can't be less than zero"); else System.out.println("Input is valid!!"); } publicstaticvoid main(String[] args) { try { employeeAge(-2); } catch (MyOwnException e){ e.printStackTrace(); } } }

More Related