Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

JAVA EXCEPTION HANDLING, Exams of Java Programming

excepJava Exception is a good topic and I recommend you to refer to this.

Typology: Exams

2019/2020

Uploaded on 12/31/2020

udai-srinivasa-prasad-k-s
udai-srinivasa-prasad-k-s 🇮🇳

1 document

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exception Exception
HandlingHandling
G.MANIKANDANG.MANIKANDAN
SAP / ICT / SOCSAP / ICT / SOC
G.Manikandan / SASTRA
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download JAVA EXCEPTION HANDLING and more Exams Java Programming in PDF only on Docsity!

ExceptionException

HandlingHandling

G.MANIKANDANG.MANIKANDAN

SAP / ICT / SOCSAP / ICT / SOC

Acknowledgement

  • Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0- 13 - 222158 - 6
  • Program images from - Schildt

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

throws

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;