0% found this document useful (0 votes)
3 views4 pages

Prgms

Uploaded by

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

Prgms

Uploaded by

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

((import java.io. is used to import all the classes from the java.

io, you gain access to a wide


range of I/O-related classes without needing to import each class individually.))
Try syntax
try {
// Code that may throw an exception
} catch (ExceptionType1 e1) {
// Code to handle ExceptionType1
} catch (ExceptionType2 e2) {
// Code to handle ExceptionType2
} finally {
// Code that will always execute (optional)
}

Try n catch
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
}

In Java, the finally keyword is used in exception handling to define a block of code that will
always execute after a try block, regardless of whether an exception was thrown or not. It’s
typically used for code that needs to be executed no matter what, such as closing files or
releasing resources.

Purpose of finally

 Ensure Execution: The code inside the finally block is guaranteed to execute whether or not
an exception occurs in the try block.
 Cleanup Resources: It's commonly used for resource cleanup, such as closing files, network
connections, or releasing database resources.

Basic Syntax
java
Copy code
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that will always execute
}

1. Program to handle Null Pointer Exception and use the “finally” method
to display a message to the user.
import java.io.*;
class prg12
{
public static void main(String[] args)
{
String ptr = null;
try
{
System.out.println("Inside try
block"); if ("gfg".equals(ptr))
System.out.println("Same");
else
System.out.println("Not Same");
}
catch(NullPointerException e)
{
System.out.println("Exception handled"); System.out.println(e);
}
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
OUTPUT:

NOTE for prg 2

((import java.awt.*; is used to include all the classes and interfaces from the
java.awt, which provides classes for creating and managing graphical user
interfaces (GUIs) in Java.))

((import javax.swing which provides a set of classes for building graphical


user interfaces (GUIs) with a richer set of components than AWT (Abstract
Window Toolkit).
((JFrame frame=new JFrame("Message Window");

is used to create a new window in a Swing-based graphical user interface (GUI)


application in Java.

((the statement frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


is used to define what should happen when the user closes the window
represented by the JFrame object.))

((he statement frame.getContentPane().add(textLabel,


BorderLayout.CENTER); is used in Swing to add a component to a container
(in this case, a JFrame), and to specify the layout position for that component.))

((The statement frame.pack(); is used in Java Swing to automatically resize


the JFrame (or any window) so that it just fits all the components that have
been added to it.))

((frame.setLocationRelativeTo(null) is used to center a window (like a


JFrame) on the screen.

((Swing is a Java toolkit for building graphical user interfaces (GUIs) for
applications and applets. ))

2. Program which create and display a message on the window


import java.awt.*;
import javax.swing.*;
public class prg13
{
private static void createwindow()
{
JFrame frame=new JFrame("Message Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel=new JLabel("Im a label in the window",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300,100));
frame.getContentPane().add(textLabel,BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
createwindow();
}
}

OUTPUT:

((The import java.awt.Graphics; statement in Java is used to import the


Graphics class from the java.awt package.)) This class provides the methods
necessary for drawing shapes, text, and images
The import javax.swing.JFrame; statement in Java imports the JFrame class
from the javax.swing package. The JFrame class is used to create a window or
frame in a graphical user interface (GUI) application.
Program to draw several shapes in the created window.
import java.awt.Graphics;
import javax.swing.JFrame;
public class shapes extends JFrame
{
public shapes()
{
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawOval(40,40,60,60);
g.drawRect(80,60,200,200);
g.drawRect(200,100,100,200);
}
public static void main(String[] args)
{
new shapes();
}
}

You might also like