






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
excepJava Exception is a good topic and I recommend you to refer to this.
Typology: Exams
1 / 30
This page cannot be seen from the preview
Don't miss anything!
Acknowledgement
Checked ExceptionChecked Exception - - ExampleExample
Checked Exception - Example import java.io.*; class filedemo { public static void main(String args[]) { FileInputStream f=new FileInputStream("filedemo.java"); int size=f.available(); System.out.print(size); } } filedemo.java
Checked Exception - Example import java.io.*; class filedemo { public static void main(String args[]) throws Exception { FileInputStream f=new FileInputStream("filedemo.java"); int size=f.available(); System.out.print(size); } } E:\slides\Java - 2020 \Unit - I>javac filedemo.java E:\slides\Java - 2020 \Unit - I>java filedemo 211 filedemo.java
Using try and catchUsing try and catch
Multiple catch ClausesMultiple catch Clauses
Multiple catch Clauses
Limitation
Error
Catch or Declare Checked Exceptions Java forces you to deal with checked exceptions. If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), you must invoke it in a try-catch block or declare to throw the exception in the calling method. For example, suppose that method p1 invokes method p2 and p2 may throw a checked exception (e.g., IOException), you have to write the code as shown in (a) or (b). void p1() { try { p2(); } catch (IOException ex) { ... } } (a) (b) void p1() throws IOException { p2(); }
Declaring Exceptions Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. public void myMethod() throws IOException public void myMethod() throws IOException, OtherException
Throwing Exceptions When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. Here is an example, throw new TheException(); TheException ex = new TheException(); throw ex;