Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
75 views37 pages

PPL Unit 6

The document discusses exception handling in Java, including: 1) Exception handling fundamentals such as exception types, uncaught exceptions, and try/catch blocks. 2) The hierarchical structure of Throwable and its subclasses like Error and Exception. 3) Common exception types like Checked Exceptions that must be handled, and Unchecked Exceptions like RuntimeExceptions. 4) Using try-catch blocks to handle exceptions, and the throw and throws keywords to propagate exceptions.

Uploaded by

Som T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views37 pages

PPL Unit 6

The document discusses exception handling in Java, including: 1) Exception handling fundamentals such as exception types, uncaught exceptions, and try/catch blocks. 2) The hierarchical structure of Throwable and its subclasses like Error and Exception. 3) Common exception types like Checked Exceptions that must be handled, and Unchecked Exceptions like RuntimeExceptions. 4) Using try-catch blocks to handle exceptions, and the throw and throws keywords to propagate exceptions.

Uploaded by

Som T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

*

UNIT-VI
Seminar Interim
Presentation
Exception – Handling
Project Department
in JAVA
of Technology
Presentation-DOT
*
R
E
S
E
A
R

Samarth College of Engineering


C
H

Belhe C
E

Prof. Ganesh D. Jadhav


N
T

E-mail: [email protected]
V
I
I
T
*
*
*

UNIT-VI Exception Handling


Seminar Presentation in JAVA
– Department
* of Technology
R
E
fundamental, exception types,
S uncaught exceptions, try, catch,
throw, throws, finally, Emultiple catch clauses, nested try
statements, built-in exceptions,
A
R
custom exceptions (creating
your own exceptionsub classes).
C

Managing I/O: Streams, HByte Streams and Character Streams,


Predefined Streams,Reading console Input, Writing Console
C
Output, Print Writer class,E
Applet: Applet Fundamental,Applet
N Architecture, Applet
Skeleton, Requesting Repainting,
T
status window, HTML
R
Applet tag, passing parameters
E
to Applets, Difference between
Applet and Application Program.
V
I
I
T
*
*
*

Seminar PresentationInheritance
– Department
* of Technology
R
E
S

Exception Handling E
A
R
C
•Exception Definition H

• Exception OccurrenceC
E
N
•Exception Handling T
R

•Exception Propagation E
V
I
I
T
*
*
*

Exception
Seminar Presentation – Department
* of Technology
R
E
•Error occurred in execution
S time
•Abnormal termination ofEprogram
A
•Wrong execution result R
•Provide an exception handling
C mechanism in language system
• Improve the reliability
H
of application program
• Allow simple program
C
code for exeception check and
handling into source
E
N
T
R
E

V
I
I
T
*
*
*

Exception Types
Seminar Presentation – Department
* of Technology
R
E
S
Checked Exception E
A

Need to be handled explicitly


R
by the code. These are
C
extended from java.lang.Exception
H
class
Example : IOException
C
E
Unchecked Exception N
Need not to be handledT explicitly JVM handles it.
Extended from java.lang.RunTimeException
R Example :
ArrayIndexOutofBound, E
NullPointerException, V
I
I
T
*
*
*

Hierarchical Structure of– Throwable


Seminar Presentation Department
* Class
of Technology
R
E

Object S
E
A

Throwable
R
C
H

Error Exception
C
E
N
T
RuntimeException
R
… E …
V
I


I
T
*
*
*

Seminar Presentation – Department


* of Technology
R
E
S
Error Class E

When a dynamic linking failure


A or other hard failure in the
R
Java virtual machine occurs, C
the virtual machine throws an
Error. Simple programs typically
H do not catch or throw
Errors.
Exception Class C
E
Most programs throw and Ncatch objects that derive from the
Exception class. An ExceptionT indicates that a problem
occurred, but it is not a serious
R
E
system problem. Most
programs you write will throw and catch Exceptions as
opposed to Errors. V
I
I
T
*
*
*

Exception
Seminar Presentation Class of Technology
– Department
*
R
E
S
E
ClassNotFoundException RuntimeTimeException IOException
A
R
C
H

EOFException E
ArithmeticException
N
FileNotFoundException
T ClassCAstException
UnknownHostException
R IllegalArgumentExceptio
MailFormedURLException
E
n
V IndexoutBoundException
I NullPointerException
I
T
*
*
*

Seminar PresentationException
– Department
* of Technology
R
E
•Using try-catch, throw, throws,
S finally mechanism
•Try block may be followed E with one or more catch/finally
block A
R
•If no error occurs in try block
C catch block is bypassed.
•If error occurs in try block
H , java creates an object of a
particular type and throw it.
C
• It check for matching E catch block exists.
N
T
R
E

V
I
I
T
*
*
*

Seminar PresentationExample
– Department
* of Technology
R
E
try
S
{
E
// sample code
A
}
R
catch(object)
C
{
H
// message
}
C
E
N
T
R
E

V
I
I
T
*
*
*
Example
Seminar Presentation – Department
* of Technology
R
E
Class exception_test S

{ E

public static void main(String


A
R
args[ ])
{ C

try H

{
C
int x=20, y=0, Z; E
System.out.println(“Before
N error”);
z = x/y; T
R
System.out.println.(“After
E
error”);
}
catch(ArithmeticException
V
ae)
I
{ System.out.println(“Don’t
I
divide by Zero”);
} T

System.out.println(“After
* try catch block”);
*
}}
*

Java’s Built-in
Seminar Presentation Exceptions
– Department
* of Technology
R
E
Exception Meaning
S

ArithmeticException Arithmetic
E error such as divide by
A
R
zero
ArrayStoreException Storing
C wrong data type in array
ArrayIndexOutOfBoundsException
H Array index is out
of Bound
C
E
FileNotFoundException N Attempt to access non existed file
IOException T
General input/output error
R
SecurityException E
Attempt to violate security
NumberFormatException Invalid conversion
V
I
I
T
*
*
*

Use of
Seminar Presentation Exception
– Department
* of Technology
R
E

•Catches all exception S


E
A
try R

{ C

// sample code H

} C

catch (Exception e) E

{ N
T
// message R
} E

V
I
I
T
*
*
*
Exception Occurrence
Seminar Presentation – Department
* of Technology
R
E
•Raised implicitly by system
S
•Raised explicitly by programmer
E

•Throw Statement A
R
C Throwable
H class or its
C
sub class
E
N
throw ThrowableObject;
T
R
E

V
I
I
T
*
*
*

Seminar Presentation – Department


*
Exception Occurrence of Technology
R
E
class ThrowStatement extendsS Exception {
public static void exp(int
E ptr) {
if (ptr == 0) A
R
throw new NullPointerException();
C

} H

public static void main(String[] args)


C
{ E
N

int i = 0; T
R
ThrowStatement.exp(i);
E
}
} V
I

java.lang.NullPointerException
I
T
at ThrowStatement.exp(ThrowStatement.java:4)
*
at ThrowStatement.main(ThrowStatement.java:8
*
*

Seminar Presentationfinally
– Department
* of Technology
R
E
•It executes whether or not San exception is thrown.
•Finally block is optional E
•Each try block must have either
A
one catch or finally block.
R
•Generally used to free resources.
C
H

C
E
N
T
R
E

V
I
I
T
*
*
*

Example
Seminar Presentation * - Finally
– Department of Technology
R
E
Public class finallyDemo S
{ E

Public static void main(Srting


A
R
args[])
{ C

try{ H

System.out.println(“Inside A”);
C
throw new IOException(“Sample
E throw”);
} N

finally T
R
{ E
System.out.println(“Finally of A”);
} V
I
} I
} T
*
*
*

JAVA
Seminar Presentation * I/O
– Department of Technology
R
E
•Import java.io.*; S

•I/O E
A
R
Console based Abstract
C window toolkit (AWT)
H

•Java performs I/O throughC stream


E
N
T
R
E

V
I
I
T
*
*
*

Seminar Presentation – Department


*
Stream of Technology
R
E
S
Stream
E
A
R
C
H

Byte stream Character stream


C
E

InputStream OutputStream
N
Reader Writer
T

BufferedInputStream BufferedOutputStreaR BufferedReader BufferedWriter


E
ByteArrayInputStrea m
m DataInputStream ByteArrayOutputStr CharArrayRead CharArrayWrit
FileInputStream eam V er FileReader er FileWriter
StringBufferInputStr DataOutputStream I
eam FileOutputStream I
ObjectOuputStream T
*
*
*

Seminar Presentation – Department


* of Technology
R
E
Byte Stream S Character Stream
The byte stream is used forE
The character stream is used
A
inputting and outputting the
R
for inputting and outputting
bytes C characters
Super classes are Inputstream
H
There are two super classes
and Outputstream C
used in byte stream and
E those are - Reader and writer
A byte is a 8-bit number type
N
A charcter is a 16-bit
T
number R
that can represent values Efrom type that represent Unicode
-127 to 127
V
I
I
T
*
*
*

Seminar Presentation – Department


* of Technology
R
E
the following three standard streams
S

Standard Input − This is usedE to feed the data to user's program
and usually a keyboard is usedA as standard input stream and
represented as System.in. R
C

Standard Output − This is usedH to output the data produced by the


user's program and usually a computer screen is used for standard
output stream and representedCas System.out.
E
N
Standard Error − This is used to output the error data produced by
T
the user's program and usuallyRa computer screen is used for
standard error stream and represented
E
as System.err.

V
I
I
T
*
*
*

Reading
Seminar Console Input
Presentation * using BufferedReader
– Department of Technology
R
E
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
S
char c;
E
c = (char) br.read(); // character reading
A
System.out.println( c );
R
String str;
C
str= br.readLine(); // string reading
H
int n;
n = Integer.parseInt(str); // integer reading
C
E
N
T
R
E

V
I
I
T
*
*
*
Writing Console output
Seminar Presentation – Department
* of Technology
R
E
S
E
A
System.out.write(val); R
C
H

System.out.write(“\n”); C
E
N
T
R
E

V
I
I
T
*
*
*

Print –writer
Seminar Presentation class of Technology
Department
*
R
E
S
Used to write contents onE the console Printwriter class is
character base A
R
C
Constructor H
PrintWriter(OutputStream output, boolean flushnewline)
C
E
Class supports for print and
N
println method
T

The writer object for PrintWriter


R class is created as follows
PrintWriter pw = new PrintWriter(System.out,
E
true);
V
I
I
T
*
*
*

Seminar Presentation – Department


* of Technology
R
E
Other data Types in JAVAS
Import java.io.*;
E
Public class demo A
{ R

Public static void main(String


C
[ ] args)
H
{
Char c = 'A'; C

Int val = 100; E


N
PrintWriter pw = new
T
PrintWriter(System.out, true);
pw.println(c); R

pw.println(val); E
} V
} I
I
T
*
*
*

Seminar Applets
Presentation – Department
* of Technology
R
E

•Applets are client side programming


S
E
•Applets are interactive Awith user.
•Applets are small it takes
R less time to execute
•Applets supports AWTC
H
•No console based input
C
E
N
T
R
E

V
I
I
T
*
*
*

Life cycle
Seminar Presentation * of Applet of Technology
– Department
R

•Init() – initialization E
S
• It loads applet in memory.
E

• Called for only once. A

•Start() l Actually start execution


R
C
of audio or video file.
Start can be called for manyH times
•Paint()
• Paint must be calledC to draw applet in graphics mode
E
•Stop() N
•Destroy() T

• Removes applet from R


memory
E

V
I
I
T
*
*
*

Applet
Seminar Presentation *Skeleton
– Department of Technology
R
E
//java file
S
import java.applet.*;
E
public class appskl extends Applet A
{// called first R
public void init() C
{// initialization;} H
// called second , after init()
public void start(){// start execution}
C //
called when applet stopped E
public void stop(){….} N

// called when applet windowT is termininated


public void destroy() {…..} R
E
//called when applet window must be restored
public void paint(Graphics g)V
{ // redisplay contents of window
I
} I
T
*
*
*
Applet
Seminar Presentation *program
– Department of Technology
R
E
S
E

//java file A
R
import java.applet.*; C
public class welcome extends
H Applet
{
C
public void paint(Graphics
E
g)
{ N

g.drawString(“FirstT Applet Program”, 10,10);


} R
E

} V
I
// HTML file I
<applet code=welcome width=300
T height=150>
</applet> *
*
*

Seminar Presentation –output


Department
* of Technology
R
E
S
E
A
R
C
H

C
E
N
T
R
E

V
I
I
T
*
*
*
How to Execute
Seminar Presentation Applet?
– Department
* of Technology
R
E
S
•1) in WebBrowser E
A

•2) appletviewer welcome.html


R
C
H

C
E
N
T
R
E

V
I
I
T
*
*
*
Applet
Seminar Presentation *program
– Department of Technology
R
E
S
import java.applet.*; E
public class app_color extends
A Applet
{ R
C
String msg; H
public void init()
{ setBackground(Color.red);
C
E
msg = “Inside init()”;N
} T

public void paint(Graphics


R g)
{ E

msg = “InsideVpaint()”;
g.drawString(msg,
I 10,30);
} I
T
} *
*
*

Seminar PresentationExample
– Department
* of Technology
R
E
S
package mypack; E
public class Balance A

{ R
C
String name; H
double bal;
public Balance(StringC n, double b)
E
{ N
name = n; T

bal = b; R

} E

public void show() V


{ I

S.o.p.(name +“ ”+bal);
I
T
} *
} *
*
Importing
Seminar package in sample
Presentation – Department
* of Technology
R
E
S
import mypack.*; E
public class sample A

{ R
C
public static void main(String
H
args[])
{
Balance b = new Balance(“sham",20000);
C
E
b.show(); N
} T

} R
E

NOTE : 1) Sample storedV outside of mypack.


2) set classpath upto root Iof mypack
I
T
*
*
*
output
Seminar Presentation – Department
* of Technology
R
E
S
E
A
R
C
H

C
E
N
T
R
E

V
I
I
T
*
*
*
Requesting Repaintaing
Seminar Presentation – Department
* of Technology
R
E
•Applet writes to window Swhen paint method is called
•When applet want to update
E
infromation that has to
A
displayed in window, it calls
R repaint.
C

Repaint()-> update()->paint()
H

C
Repaint method call ways:E
1. void repaint( ) // EntireN window repainted
T
2. void repaint(int left, intRtop, int width, int height) //
specified pixel repainted. E
3. void repaint(long maxDelay) //repaint after specified
V
delay I
4. void repaint((long time,I int left, int top, int width, int
height) //specified pixel repainted
T with speciifed pixel
*
delay *
*
Status Window
Seminar Presentation – Department
* of Technology
R

•Applet uses statuwindowES to display information. It can done


by calling method showStatus
E ()
import java.applet.*; A
R
public class app_color extends
C
Applet
{ H

String msg;
C
public void init() E
{ setBackground(Color.red);
N

msg = “Inside init()”;


T

} R
E
public void paint(Graphics g)
{ V

msg = “Inside paint()”;


I
I
g.drawString(msg,T 10,30);
showStatus(“Status:Applet
* is running fine”);
}} *

You might also like