Professional Documents
Culture Documents
Exceptions, Threads and Applets: Accent e Technology Pvt. LTD
Exceptions, Threads and Applets: Accent e Technology Pvt. LTD
Exception Handling
An exception is an abnormal condition that arises in
a code sequence at run time. That is, an exception is
a runtime error.
When an exceptional condition arises, an object
representing that exception is created and thrown in
the method that caused the error.
Java exception handling is managed through five
keywords: try, catch, throw, throws, and finally.
Exception-handling block
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
//Following Block is Optional
finally {
// block of code to be executed before try block ends
}
throw
To manually throw an exception, we use the
keyword throw.
The general form of throw is,
throw ThrowableInstance;
throw - Example
class ThrowDemo
{
public void countDown()
{
try
{
for (int i=10; i>0; i--)
{
if(i<=5)
throw new LowException (i);
System.out.println("Value is "+i);
}
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
throw - Example
class LowException extends Exception
{
int f;
LowException(int f)
{
this.f = f;
}
public String toString()
{
return "Minimum Balance Reached - Value is "+f;
}
}
class DemoThrow
{
public static void main(String a[])
{
ThrowDemo tdOb = new ThrowDemo();
tdOb.countDown();
}
}
throws
A throws clause lists the types of exceptions that a
method might throw.
The general form is,
type method-name(parameter-list) throws exception-list
{
// body of method.
}
throws - Example
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
ArithmeticException
ArrayIndexOutOfBoundsException
IllegalArgumentException
NullPointerException
checked exceptions
ClassNotFoundException
IllegalAccessException
NoSuchFieldException
NoSuchMethodException
Multithreaded Programming
A multithreaded program contains two or more parts
that can run concurrently.
Multithreading is a specialized form of multitasking.
Multithreading enables you to write very efficient
programs that make maximum use of the CPU.
To create a new thread, your program will either
extend Thread or implement the Runnable
interface.
When a Java program starts up, one thread begins
running immediately.
Thread class
The Thread class defines several methods that help
manage threads.
Implementing Runnable
class NewThread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
Implementing Runnable
public void run()
{
try
{
for(int i = 5; i > 0; i)
{
System.out.println("Child Thread: " + i);
Thread.sleep (500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
Implementing Runnable
class ThreadDemo
{
public static void main(String args[])
{
new NewThread();
try
{
for(int i = 5; i > 0; i)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Extending Thread
class NewThread1 extends Thread
{
NewThread1()
{
super("Demo Thread");
System.out.println("Child thread: " + this);
start();
}
Extending Thread
public void run()
{
try
{
for(int i = 5; i > 0; i)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
Extending Thread
class ExtendThread
{
public static void main(String args[])
{
new NewThread1();
try
{
for(int i = 5; i > 0; i)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Applet
Applets are small applications that are accessed on
an Internet server, transported over the Internet,
automatically installed, and run as part of a Web
document.
Every applet that you create must be a subclass of
class Applet.
Applets do not begin execution at main( ).
An applet begins execution when the name of its
class is passed to an applet viewer or to a network
browser.
Applet
There are two ways in which you can run an applet:
Executing the applet within a Java-compatible Web
browser.
Using an applet viewer, such as the standard JDK tool,
appletviewer.
Applet - Example
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleApplet" width=200 height=60>
</applet>
*/
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.cyan);
g.drawString("A Simple Applet", 20, 20);
g.drawLine(20,30,105,30);
}
}