Professional Documents
Culture Documents
Java PPT - 4 by Adi
Java PPT - 4 by Adi
Unit-4
Dr. K ADISESHA
Exception Handling 2
& Applets in Java
Introduction
Exception
Exception Handling
Applets Programming
Prof. K. Adisesha
3
Introduction
Exception in Java:
An exception (or exceptional event) is a problem that arises during the execution of a
program.
➢ When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
➢ An exception can occur for many different reasons. Following are some scenarios where
an exception occurs.
❖ A user has entered an 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.
Prof. K. Adisesha
4
Introduction
Java - Applet:
An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
➢ There are some important differences between an applet and a standalone Java application:
❖ An applet is a Java class that extends the java.applet.Applet class.
❖ A main() method is not invoked on an applet, and an applet class will not define main().
❖ Applets are designed to be embedded within an HTML page.
❖ When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
❖ A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or
a separate runtime environment.
❖ The JVM on the user's machine creates an instance of the applet class and invokes various
methods during the applet's lifetime.
Prof. K. Adisesha
5
Exception in Java
Exception Hierarchy:
All exception classes are subtypes of the java.lang.Exception class.
➢ The Exception class and Error is a subclass of the Throwable class.
➢ Errors are abnormal conditions that happen in
case of severe failures, these are not handled
by the Java programs.
➢ The Exception class has two main subclasses:
❖ IOException class
❖ RuntimeException Class.
Prof. K. Adisesha
6
Exception in Java
Prof. K. Adisesha
7
Exception in Java
Prof. K. Adisesha
10
Exception Handling in Java
Catching Exceptions:
A method catches an exception using a combination of the try and catch keywords.
➢ A try/catch block is placed around the code that might generate an exception.
➢ Code within a try/catch block is referred to as protected code, and the syntax for using
try/catch looks like the following. Syntax:
➢ The code which is prone to exceptions is placed in the try {
try block. // Protected code
➢ When an exception occurs, that exception occurred is }
handled by catch block associated with it. catch (ExceptionName e1)
➢ Every try block should be immediately followed either {
// Catch block
by a catch block or finally block.
}
Prof. K. Adisesha
11
Exception Handling in Java
Java try and catch:
When an error occurs, Java will normally stop and generate an error message. The
technical term for this is: Java will throw an exception (throw an error).
➢ The try statement allows you to define a block of code to be tested for errors while it is being
executed.
➢ The catch statement allows you to define a block of code to be executed, if an error occurs in
the try block. Example:
➢ The try and catch keywords come in pairs:. class JavaException
{ public static void main(String args[])
Syntax: try { { int d = 0; int n = 20;
// Block of code to try try { int fraction = n / d;
} System.out.println("This line will not be Executed"); }
catch(Exception e) { catch (ArithmeticException e)
{ System.out.println("In the catch Block due to Exception = " + e);}
// Block of code to handle errors
Prof. K. Adisesha System.out.println("End Of Main"); } }
}
12
Exception Handling in Java
Java Applet:
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
➢ An applet is a Java class that extends the java.applet.Applet class.
➢ A main() method is not invoked on an applet, and an applet class will not define main().
➢ Applets are designed to be embedded within an HTML page.
➢ Advantage of Applet
❖ There are many advantages of applet. They are as follows:
❖ It works at client side so less response time.
❖ Secured
❖ It can be executed by browsers running under many plateforms, including Linux, Windows,
Mac OS etc.
Prof. K. Adisesha
17
Java Applet
Java Applet:
Java Applets are programs stored on a web server, similar to web pages:
➢ When an applet is referred to in a web page that has been fetched and processed by a browser,
the browser generates a request to fetch (or download) the applet program, then executes the
program in the browser’s execution context, on the client host.
server host
browser host
web server
browser
reqeust for
myWebPage.html
myWebPage.html
myWebPage.html HelloWorld.class
... request for
<applet code=HelloWorld.class</applet> HelloWorldclass
...
HelloWorld.class HelloWorld.class
Prof. K. Adisesha
18
Java Applet
Applet Execution :
An applet program is a written as a subclass of the java.Applet class or the
javax.swing.Japplet class.
➢ There is no main method: you must override the start method. Applet objects uses AWT for
graphics. JApplet uses SWING.
➢ It is a Grapics object that runs in a Thread object, so every applet can perform graphics, and
runs in parallel to the browser process.
➢ When the applet is loaded, these methods are automatically invoked in order:
❖ The init( ) method is invoked by the Java Virtual Machine.
❖ The start( ) method
❖ The paint( ) method
Prof. K. Adisesha
19
Java Applet
Java Applet:
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
➢ Lifecycle of Java Applet
❖ Applet is initialized.
❖ Applet is started.
❖ Applet is painted.
❖ Applet is stopped.
❖ Applet is destroyed.
Prof. K. Adisesha
20
Java Applet
Invoking an Applet:
An applet may be invoked by embedding directives in an HTML file and viewing the file
through an applet viewer or Java-enabled browser.
➢ The <applet> tag is the basis for embedding an applet in an HTML file. Following is an
example that invokes the "Hello, World" applet −
<html>
<title>The Hello, World Applet</title>
<hr>
<applet code = "HelloApplet.class" width = "320" height = "120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
<hr>
Prof. K. Adisesha </html>
25
Java Applet
Running an Applet:
An applet may be invoked by embedding directives in an HTML file and viewing the file
through an applet viewer or Java-enabled browser.
➢ There are two ways to run an applet
➢ HTML file: To execute the applet by html file, create an applet and compile it.
❖ After that create an html file and place the applet code in html file.
❖ Now click the html file.
➢ appletViewer tool: To execute the applet by appletviewer tool, create an applet that contains
applet tag in comment and compile it.
❖ After that run it by: appletviewer File.java.
Prof. K. Adisesha
26
Java Applet
Running an Applet:
HTML file: To execute the applet by html file, create an applet and compile it. After that
create an html file and place the applet code in html file. Now click the html file.
//First.java //myapplet.html
import java.applet.Applet; <html>
import java.awt.Graphics; <body>
public class First extends Applet <applet code="First.class" width="300" height="300">
{ </applet>
public void paint(Graphics g) </body>
{ g.drawString("welcome",150,150); } </html>
}
Prof. K. Adisesha
27
Java Applet
Running an Applet:
Applet by applet viewer tool: To execute the applet by appletviewer tool, create an applet that
contains applet tag in comment and compile it. After that run it by: appletviewer First.java.
//First.java /*
import java.applet.Applet; <applet code="First.class" width="300" height="300">
import java.awt.Graphics; </applet>
public class First extends Applet */
{
public void paint(Graphics g)
{ g.drawString("welcome to applet",150,150); } To execute the applet by appletviewer tool, write in command
prompt:
} c:\>javac First.java
c:\>appletviewer First.java
Prof. K. Adisesha
28
Java Applet
The HelloWorld Applet: public void paint(Graphics g){
<HTML>
<BODY>
final int FONT_SIZE = 42;
<APPLET code=hello.class width=900 height=300> Font font = new Font("Serif", Font.BOLD, FONT_SIZE);
</APPLET>
</BODY> // set font, and color and display message on
</HTML>
// the screen at position 250,150
// applet to display a message in a window
g.setFont(font);
import java.awt.*;
g.setColor(Color.blue);
import java.applet.*;
// The message in the next line is the one you will see
public class hello extends Applet{
g.drawString("Hello, world!",250,150);
public void init( ){
}
setBackground(Color.yellow);
Prof. K. Adisesha
}
}
29
Java Applet
Advanced Applets:
You can use threads in an applet.
➢ You can make socket calls in an applet, subject to the security constraints.
➢ A proxy server can be used to circumvent the security constraints.
Server host Client host Server host Client host
HTTP server browser HTTP server browser
forbidden Host X
Host X connection request
connection request
server Z
server Z
Prof. K. Adisesha
30
Discussion
Queries ?
Prof. K. Adisesha
9449081542