0% found this document useful (0 votes)
9 views

Java long questions

An applet in Java is a small application designed to run in a web browser, with a lifecycle consisting of methods such as init(), start(), stop(), destroy(), and paint(). Exception handling in Java involves using try, catch, finally, throw, and throws keywords to manage errors during program execution. The document includes examples of a simple applet and exception handling, demonstrating how to implement these concepts in Java code.

Uploaded by

Prabesh Thapa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java long questions

An applet in Java is a small application designed to run in a web browser, with a lifecycle consisting of methods such as init(), start(), stop(), destroy(), and paint(). Exception handling in Java involves using try, catch, finally, throw, and throws keywords to manage errors during program execution. The document includes examples of a simple applet and exception handling, demonstrating how to implement these concepts in Java code.

Uploaded by

Prabesh Thapa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Applet in Java

An applet is a small application written in Java, designed to be run in a web browser or an applet
viewer. It is embedded in an HTML page and executed by the Java Virtual Machine (JVM) in
the context of a browser.

Applet Lifecycle

The lifecycle of an applet includes the following methods, which are called in a specific
sequence:

1. init(): This method is called once when the applet is first loaded. It is used for
initialization.
2. start(): This method is called after init(), and every time the applet is revisited in the
browser. It is used to start or resume the applet’s execution.
3. stop(): This method is called when the applet is no longer visible or the user navigates
away. It is used to stop any processing started by start().
4. destroy(): This method is called when the applet is being destroyed, such as when the
browser is closed. It is used to clean up resources before the applet is removed from
memory.
5. paint(Graphics g): This method is called whenever the applet needs to redraw its output.
It is often used to render the applet’s graphics.

Simple Applet Example

import java.applet.Applet;
import java.awt.Graphics;

public class SimpleApplet extends Applet {


public void init() {
// Initialization code
System.out.println("Applet initialized");
}

public void start() {


// Code to start or resume the applet
System.out.println("Applet started");
}

public void stop() {


// Code to stop the applet
System.out.println("Applet stopped");
}

public void destroy() {


// Cleanup code
System.out.println("Applet destroyed");
}

public void paint(Graphics g) {


// Code to paint the applet
g.drawString("Hello, World!", 20, 20);
}
}

To run this applet, you need an HTML file to load it:

<html>
<body>
<applet code="SimpleApplet.class" width="300" height="300">
</applet>
</body>
</html>

Exception in Java

An exception in Java is an event that disrupts the normal flow of the program’s instructions
during execution. It is an object that is thrown at runtime when an error or other unusual
condition occurs.

Mechanism of Exception Handling

Java provides a powerful mechanism for handling exceptions using a combination of try, catch,
finally, throw, and throws keywords.

1. try block: Code that might throw an exception is placed inside a try block.
2. catch block: Code to handle the exception is placed inside a catch block. It catches
exceptions of a specific type.
3. finally block: Code that is always executed, regardless of whether an exception is thrown
or not. It is typically used for cleanup activities.
4. throw statement: Used to explicitly throw an exception.
5. throws keyword: Used in a method signature to declare the types of exceptions that the
method might throw.
Example of Exception Handling

java
Copy code
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]); // This will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("Finally block executed.");
}

// Example of throw and throws


try {
validateAge(15);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
}

public static void validateAge(int age) throws Exception {


if (age < 18) {
throw new Exception("Age is not valid to vote.");
} else {
System.out.println("Welcome to vote.");
}
}
}

In this example:

 The try block contains code that may throw an ArrayIndexOutOfBoundsException.


 The catch block handles the exception and prints a message.
 The finally block always executes, ensuring that cleanup code runs regardless of whether
an exception is thrown.
 The validateAge method demonstrates the use of throw and throws for custom exception
handling.

You might also like