UNIT-IV
1. What is exception?
It is an unexpected event that occurs during the execution of a program that disrupts
normal flow of a program.
Or
An exception is an error that occurs during runtime.
2. How user defined exception is done?
User defined exception is also known as custom exception
User can create their own exception class and throws that exception using
“throw” keyword.
3. Write down the applet code for “hello-class”file?
import java.applet.Applet;
import java.awt.Graphics;
public class HelloCLASSApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello CLASS", 50, 50);
}
}
4. Why repaint () method is used?
This method is used to call the update() method internally that calls the paint()
method to repaint the component.
5. What is exception handling?
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
6. What is the need for ‘applet viewer’?
An applet viewer is tool provided with the standard java JDK to execute an applet.
7. What is error? Compare with exception.
Errors occur in the java virtual machine itself, whereas exception in a program.
These types of exceptions are beyond our control, and a program will not handle
them.
An exception is an error which can be handled .An error is an error which cannot
be handled.
8. What is the purpose of ‘init ()’ method in applet?
init()- is called to initialize the applet before it gets loaded.
9. Define an exception. How is exception handling done in java?
It is an unexpected event that occurs during the execution of a program that disrupts
normal flow of a program.
The try-catch is the simplest method of handling exceptions.
10. What is the difference between error and exception?
Error Exception
Classified as an unchecked type Classified as checked and unchecked
It belongs to java.lang.error It belongs to java.lang.Exception
It is irrecoverable It is recoverable
It can't be occur at compile time It can occur at run time compile time
both
OutOfMemoryError ,IOError NullPointerException , SqlException
11. How applets differ from applications?
Java Application Java Applet
Applications are Java programs Applets are small Java programs designed
executed without using the web to be included with the HTML web
browser. document. They require a web browser
for execution
Application program requires main() Applets doesn’t require.
method for its execution
12. How exceptions are classified in Java?
Exceptions can be categorized into two ways:
Built-in Exceptions
Checked Exception
Unchecked Exception
User-Defined Exceptions
Built-in Exception:
Exceptions already available in Java libraries are referred to as built-in exception.
It can be categorized into two categories,
checked exceptions
unchecked exception.
Checked Exception
Checked exceptions are called compile-time exceptions
Exceptions are checked at compile-time by the compiler.
These exceptions are handled using try-catch blocks.
Checked exceptions are extended from Java.lang.Exception class
Unchecked Exceptions
These exceptions are not handled in the program code.
JVM handles unchecked exceptions.
Unchecked exceptions are extended from Java.lang.RuntimeException class
13. What is use of throw keyword?
The java throw keyword is used to throw exception explicitly.
14. Mention the attributes of PARAM tag?
<param> tag: is used for passing parameters to an embedded object using <object>
tag.
Attribute Value
name parameter type
type MIME type
value value
Value type data
ref
object
15. Explain <APPLET> tag with an example.
<applet> tag was used to embed the Java applet in an HTML document.
Applet tag <applet> is required to load and start an applet either in a browser
window or in an appletviewer(provided by JDK)
An applet is embedded in an HTML page using the APPLET or OBJECT tag and
hosted on a web server.
The <applet> tag supports all global and event attributes.
Attribute name Value Description
code URL It specifies the URL of Java applet class file.
width pixels It specifies the display width of the applet panel.
height pixels It specifies the display height of applet panel
Example : adding applet to html file
<html>
<body>
<p>Example of Applet Tag</p>
<applet code="Shapes.class" align="right" height="200" width="300">
<b>Welcome to java </b>
</applet>
</body>
</html>
16. Write a program to illustrate try, catch and finally statement.
public class ExcepTest
{
public static void main(String args[])
{
int a[] = new int[2];
try
{
System.out.println("Access element three :" + a[3]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown :" + e);
}
Finally
{
a[0] = 6;
System.out.println("First element value: " + a[0]);
System.out.println("The finally statement is executed");
}
}
}
17. What is applet? Explain applet life cycle with a neat diagram.
An applet is a Java program that can be embedded into a web page.
It runs inside the web browser and works at client side.
Life Cycle of an applet
The process of how the object is created, started, stopped, and destroyed during
the entire execution of its application.
Stages in the life cycle of java applet.
Initializing an applet
Starting an applet.
Painting an applet
Stopping an applet
Destroying an applet
It basically has five core methods namely
init()
start()
stop()
paint()
destroy().
These methods are accessed by the browser to execute.
Step 1: Initialization
Public void init()
Every applet will start its execution from init() method.
It is executed only once.
There is no main() method like normal java programs.
Step 2: Start
Public void start()
After init() method start() method is invoked
Executed when the browser is maximized(loading or redreshing)
Step 3: Paint
Public void paint(graphics g)
Paint method is used to display content on the applet.
It will take graphics class as a parameter/arguments
We can create the objects or components to the applet. Or directly write a message on
applet.
Step 4: Stop
Public void stop()
Stop() method is used to stop the applet.
It is executed when the browser is minimized.
Step 5: Destroy
Public void destroy()
Destroy() method is used to completely close the applet.
It is executed when the applet is closed.
18. Write a program to implement mouse events.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class AppletMouseListener extends Applet implements MouseListener
{
String str="";
public void init()
{
public void mousePressed(MouseEvent e)
{
str = "You pressed mouse";
repaint();
}
public void mouseReleased(MouseEvent e)
{
str = "You released mouse";
repaint();
}
public void mouseClicked(MouseEvent e)
{
str = "You clicked mouse";
repaint();
}
public void mouseEntered(MouseEvent e)
{
str = "Mouse entered frame";
repaint();
}
public void mouseExited(MouseEvent e)
{
str = "Mouse existed frame";
repaint();
}
public void paint(Graphics g)
{
g.drawString(str, 75, 150);
}
}
19. Explain the steps to of executing an applet using a simple code.
Step1: Writing applet code
Create the applet program called MyFirstApplet as shown
import java.applet.Applet;
import java.awt.Graphics;
public class MyFirstApplet extends Applet
{
public void paint(graphics g)
{
g.drawString(“hello applet”, 20, 20);
}
}
Step2: Compile applet code and generate byte code.
Save the applet code and generate the byte code using java compiler javac
Step3: Create an HTML page.
<HTML>
<HEAD>
<TITLE> this is my first applet </TITLE>
</HEAD>
<BODY>
<applet code=MyFirstApplet.class width= 400 height=500>
</Applet>
</BODY>
</HTML>
Step4: Executing an applet using appletviewer
There are two ways in which we can run an applet:
Inside a browser
Using appletviewer.
The appletviewer is much easier to use during development.
Executing an applet using appletviewer
Appletviewer appletExample.html
20. Explain try, catch with example.
The core of exception handling is try and catch .
The try and catch keywords come in pairs
General form of the try/catch exception handling blocks
try
{
// do risky things
}
catch(Exception ex)
{
//try to recover
}
Try
The try block allows you to define a block of code to be tested for errors while it is being
executed.
It must be used within the method
Catch
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter.
The declared exception must be the parent class exception
The catch block must be used after the try block only.
You can use multiple catch block with a single try block.
Example:
public class Demo
{
public static void main(String[] args)
{
try
{
int data=50/0; //may throw exception
}
catch(ArithmeticException e) //handling the exception
{
System.out.println(e);
}
System.out.println("rest of the code");
}
21. Explain user defined exception in java.
Java provides us to create our own exceptions which are derived classes of
exception.
Things to remember before writing an exception
All exceptions must be a child of Throwable
To write a runtime exception, extend the RuntimeException class.
Steps to define our own exception.
Extend the exception class
Setup the constructor. That takes variable to tell the user that the number is
incorrect.
Create the function that returns the error to the user and override toString( )
method.
When the error occurs, the code throws an exception.
Example:
class MyException extends Exception
{
String str1;
MyException(String str2)
{
str1=str2;
}
public String toString()
{
return(“MyException Occurred:”+str1);
}
}
class Example1
{
public static void main(String args[])
{
Try
{
System.out.println(“Starting of try block”);
throw new MyException(“This is my error message”);
}
catch(MyException exp)
{
System.out.println(“Catch block”);
System.out.println(exp);
}
}
}
22. Explain how parameters passed to an applet.
Applet can get different input from the HTML file that contains the <APPLET>tag
through the use of applet parameter. To set up and handle parameters in the applet ,
we need two things:
1. A special parameter tag in the HTML file.
2. Code in our applet to read those parameters
Example:
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
public class MyFontApplet extends Applet
{
String fontName;
int fontSize;
public void init()
{
fontName = getParameter(“font”);
fontSize = Integer.parseInt(getParameter(“size”));
}
Public void paint (Graphics g)
{
Font f = new Font(fontName and fontSize);
g.setFont(f);
g.drawString(“Skyward Publishers”,50,50);
}
}.
23. How to display numeric values in an applet?
In Applet, we can display numerical values .
Convert numerical value into strings .
Using the drawString() method of Graphics class. We can do this easily by calling
the valueOf() method of String class.
Example:
import java.awt.*;
import java.applet.*;
public class temp extends Applet
{
public void paint (Graphics g)
{
int x=10;
int y=25;
int add=x+y;
String s="add:"+String.valueOf(add);
g.drawString(s, 100, 100);
}
}
24. Explain update() and repaint() methods.
Repaint():
The repaint() method belongs to Component class that lies into java.awt
package.
This method is used to call the update() method internally that calls the paint()
method to repaint the component.
The paint() and repaint() both are used to paint a component.
but the repaint() method internally calls paint() to paint the component.
Repaint() method cannot be overridden because it is a final method in applet
class.
Update ():
Whenever a screen needs redrawing, update( ) method is called.
By default, the update() method clears the screen and then calls the paint() method
25. What are the different ways of executing an applet?
Applets are small java programs.
They can be viewed on an internet server, transmitted over an internet, and run
automatically as a part of web doc or application.
There are two standard methods for running an applet:
By using web browser
By using Applet viewer
Using web browser:
If I have demo.java file where java is present.
import java.awt.*;
import java.applet.*;
public class Demo extends Applet
{
String msg="";
public void init()
{
msg="Hello , good morning";
}
public void start()
{
msg=msg+",Welcome to bangalore";
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
}
Compiling : javac demo.java
Create a HTML file and embedded the Applet tag in the HTML file
Demo.html
<html>
<body>
<applet code-“demo.java” width=300 height=100>
</applet>
</body>
</html>
Now the applet demo.class is loaded into the browser when you access demo.html
Using Applet viewer
It is a java application that allows you to view applets.
It is similar to a mini browser that enables you to see how applet appears on browser.