CS233 Module 4
CS233 Module 4
DESIGN PATTERNS
(OOPDP)
Shamama Anwar
Assistant Professor
Department of Computer Science and Engineering
Birla Institute of Technology, Mesra.
[email protected]
9431103880
Module - IV
Exception Handling and GUI Design
Exception Handling
• When to use exception handling
• Java exception hierarchy
• finally block
• Stack unwinding
• Chained exceptions, Declaring new exception types, Assertions, try
with resources.
GUI Design
• Simple I/O with GUI
• Basic GUI Components
• GUI Event handling, Adapter classes, Layout managers, using Panels
Error Exception
StringIndex ArrayIndex
OutOfBounds OutOfBounds
Exception Exception
By: Shamama Anwar
4
Exception Types
Checked Exception
• Exceptions which are checked for during compile time are called checked
exceptions.
• Exceptions which are not checked for during compile time are called
unchecked exception.
• The exceptions raised, if not handled will be handled by the Java Virtual
Machine. The Virtual machine will print the stack trace of the exception
indicating the stack of exception and the line where it was caused.
NullPointerException
String s = null;
System.out.println(s.length());
NumberFormatException
String s = “abc”;
int a = Integer.parseInt(s);
ArrayIndexOutOfBoundsException
try catch
• Can be placed within any method that can throw an exception.
• Catch block is used to catch any exception raised from the try block.
• When an exception occurs, processing continues at the first catch clause that
matches the exception type.
• At a time only one Exception occurs and at a time only one catch block is
executed.
• All catch blocks must be ordered from most specific to most general i.e. catch
for ArithmeticException must come before catch for Exception .
• It is added to the method signature to let the caller know about what
exceptions the called method can throw.
• It is the responsibility of the caller to either handle the exception or it can also
pass the exception (by specifying throws clause)
Circle.getNumberOfObjects());
}
}By: Shamama Anwar 37
Rethrowing Exception
public class RethrowingExceptions
{ static void divide()
{ int x,y,z;
try
{ x = 6 ;
y = 0 ;
z = x/y ;
System.out.println(x + "/"+ y +" = " + z); }
catch(ArithmeticException e)
{System….("Exception Caught in Divide()");
System…("Cannot Divide by Zero in Integer Division");
throw e; } }
public static void main(String[] args)
{ System.out.println("Start of main()");
try
{ divide(); }
catch(ArithmeticException e)
{ System…("Rethrown Exception Caught in Main()");
System.out.println(e);
By: Shamama Anwar } } 38
Stack Unwinding
import java.util.Scanner;
public class TakeInput
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter an integer: ");
int num=s.nextInt();
System.out.println("You entered "+num);
} }
Enter an integer: Java
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at TakeInput.main(TakeInput.java:7)
• Unwinding the method-call stack means that the method in which the exception
was not caught terminates, all local variables in that method go out of scope and
control returns to the statement that originally invoked that method.
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
Applet Application
Does not have a main() function. Execution begins from main()
Are not standalone programs. Needs Are standalone programs
to be embedded into webpages
Can only be executed in an Can be executed from command line
appletviewer or web browser
start()
stop()
RUNNING IDLE
start()
destroy()
paint()
DEAD
• setForeground(Color c)
• showStats(String s)
CODEBASE: It specifies the URL of the directory where the executable class file
will be searched for
ALT: It specifies the text to display in case the applet cannot be displayed.
NAME: gives a name to the applet
ALIGN: sets the alignment (LEFT, RIGHT, TOP, BOTTOM..)
VSPACE: specifies the space (in pixels) above and below the applet.
HSPACE: specifies the space (in pixels) on each side of the applet.
PARAM: It is used to supply user defined parameters to the applet
Component
Frame
Container Window
Dialog
Canvas Panel
Button
Checkbox
Choice
Label
List
Scrollbar
TextField
TextComponent
TextArea
By: Shamama Anwar
54
Component class
• Component is the superclass of most of the displayable classes defined within
the AWT. It is abstract.
• The Component class defines data and methods which are relevant to all
Components
• The Container class defined all the data and methods necessary for managing
groups of Components
void add(Component c)
Component getComponent(int n)
Dimension getMaximumSize()
Dimension getMinimumSize()
Dimesion getPreferredSize()
void remove(Component c)
void removeAll()
• Frame defines a top-level Window with Borders and a Menu Bar. Frames are
more commonly used than Windows
Panel class
• When writing a GUI application, the GUI portion can become quite complex.
• To manage the complexity, GUIs are broken down into groups of components.
Each group generally provides a unit of functionality.
Label
• String getText()
• void setText(String s)
Checkbox
• void addItemListener(ItemListener il)
• CheckboxGroup getCheckboxGroup()
• String getLabel()
• ItemListener[] getItemListeners()
• boolean getState()
• String paramString()
• void removeItemListener(ItemListener il)
• void setCheckboxGroup(CheckboxGroup ckbg)
• void setLabel(String label)
• void setState(boolean state)
• If you inherit the adapter class, you will not be forced to provide the
implementation of all the methods of listener interfaces.
MouseMotionListener MouseMotionAdapter
FocusListener FocusAdapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
By: Shamama Anwar
70
Adapter Classes: Example 10 (Applet)
/*……*/
import ..
public class Example extends Applet
{ int xcord, ycord;
public void init()
{ addMouseMotionListener(new MouseDemo(this)); }
public void paint(Graphics g)
{ g.drawstring(“(”+xcord+“,”+ycord+“)”,xcord,ycord);}
} }
public MouseDemo extends MouseAdapter
{ Example e;
MouseDemo(Example e) { this.e=e; }
public void mouseClicked(MouseEvent me)
{ d.xcord = me.getX(); d.ycord = me.getY();
d.repaint();
}
}