Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXCEPTION HANDLING OR ERROR HANDLING.

Similar presentations


Presentation on theme: "EXCEPTION HANDLING OR ERROR HANDLING."— Presentation transcript:

1 EXCEPTION HANDLING OR ERROR HANDLING

2 - Error means mistakes or buggs - Error is classified into types
What is Error? - Error means mistakes or buggs - Error is classified into types Error Compile Time Error Run Time Error All syntax errors will be detected And displayed by the Java compi ler and therefore these errors Are known as compile – time – errors A program may compile success fully creating the .exe file but not run properly. Such programs may produce wrong results due to wrong loaic or may terminate due to errors Missing Semicolon Missing brackets in classes and methods 3. Use of undeclared variables Dividing an integer by zero Accessing an element that is out of the bounds of an array

3 Some Example of Runtime Errors
class Error_Handling { public static void main(String args[]) int a,b,c; a = 10; b = 0; c = a / b; System.out.println("The Value of the C Value is:" + c); } Output: / by zero

4 Exception Types Throwable Exception (or) RuntimeException Error
1. All Exception types are subclasses of the built – in class Throwable. Throwable is at the top of the exception class hierarchy Throwable Exception (or) RuntimeException Error This class is used for exceptional conditions that user programs should catch. This is also the class That you will subclass to create your own custom exception types Which defines exceptions that are not Expected to be caught under normal Circumstances by our program. Ex. Stack overflow

5 Uncaught Exception class Error_Handling {
public static void main(String args[]) int i,j,k1,k2; i = 10; j = 0; k1 = i / j; k2 = i + j; System.out.println("The Division of the K1 Value is: " + k1); System.out.println("The addition of the K2 Value is: " + k2); } Output: java.lang.ArithmeticException: / by zero at Error_Handling.main(Error_Handling.java:4) In this example, we haven’t supplied any exception handlers of our own, so the exception is caught by the default handler provided by the java run – time system

6 What is Exceptions? An Exception is condition that is caused by a run – time error in the program. What is Exception Handling? If the exception object is not caught and handled properly, the compiler will display an error message and will terminate the program. If we want the program to continue with the execution of the remaining appropriate message for taking corrective actions. This task is known as exception handling. Exceptions Types Exception Asynchronous Exception Synchronous Exception Keyboard Interrupt Mouse Interrupt Division by Zero

7 Catches and handles the
Exception Handling Mechanism The purpose of the exception handling mechanism is to provide means to detect and report an “exceptional circumstance”, so that appropriate action can be taken. The Error Handling code that performs the following tasks: 1. Find the problem (Hit the Exception) 2. Inform that an error has occurred (Throw the exception) 3. Receive the error information (Catch the exception) 4. Take corrective actions (Handle the exception) try block Detects and throws an exception catch block Catches and handles the exception Exception object

8 Using try and catch //Block of statements which detects and
try { } catch(Exception_Type e) //Block of statements which detects and //throws an exception //Catches exception //Block of statements that handles the //exception

9 Example Program for Exception Handling Mechanism
class Error_Handling { public static void main(String args[]) int i,j,k1,k2; i = 10; j = 0; try k1 = i / j; System.out.println("The Division of the K1 Value is: " + k1); } catch(ArithmeticException e) System.out.println("Division by Zero"); k2 = i + j; System.out.println("The addition of the K2 Value is: " + k2); There is an Exception Catches the exception Output: Division by Zero, The addition of the K2 Value is:10

10 Multiple Catch Statements
It is possible that a program segment has more than one condition to throw an exception. try { //statements } catch(Exception-Type-1 e) catch(Exception-Type-2 e) catch(Exception-Type-N e)

11 class Error_Handling { public static void main(String args[]) int a[ ] = {5,10}; int b = 5; try int x = a[2] / b - a[1]; } catch(ArithmeticException e) System.out.println("Division by Zero"); catch(ArrayIndexOutOfBoundsException e) System.out.println("Array Index Error"); catch(ArrayStoreException e) System.out.println("Wrong data type"); int y = a[1] / a[0]; System.out.println("Y = " + y);

12 COMMAN EXCEPTION HANDLER
class Error_Handling { public static void main(String args[]) int a[ ] = {5,10}; int b = 5; try int x = a[2] / b - a[1]; } catch(ArithmeticException e) System.out.println("Division by Zero"); /*catch(ArrayIndexOutOfBoundsException e) System.out.println("Array Index Error"); }*/ catch(ArrayStoreException e) System.out.println("Wrong data type");

13 catch(Exception e) { System.out.println("The Producing any Runtime Error" e.getMessage()); } int y = a[1] / a[0]; System.out.println("Y = " + y);

14 EXCEPTION HIERARCHY class Error_Handling { public static void main(String args[]) int a[ ] = {5,10}; int b = 5; try int x = a[2] / b - a[1]; } catch(ArithmeticException e) System.out.println("Division by Zero"); catch(Exception e) System.out.println("The Producing any Runtime Error" e.getMessage()); /*catch(ArrayIndexOutOfBoundsException e) System.out.println("Array Index Error"); }*/

15 catch(ArrayStoreException e)
{ System.out.println("Wrong data type"); } int y = a[1] / a[0]; System.out.println("Y = " + y);

16 Nested try Statements class Error_Handling {
public static void main(String args[]) try int a = args.length; int b = 42 / a; //If no command - line args are present, //the following statement will generate a divide - by - zero exception System.out.println("a = " + a); // nested try bloack // If one command - line arg is used, then a divide - by //- zero exception will be generated by the following code.

17 try { if ( a == 1) a = a / (a - a); //division by zero if(a == 2) int c[] = {1}; c[42] = 99; //generate an out - of - bounds exception } catch(ArrayIndexOutOfBoundsException e) System.out.println("Array index out - of - bounds: " + e); catch(ArithmeticException e) System.out.println("Divide by Zero: " + e);

18 Nested try Statements using Subroutine
class Error_Handling { static void nesttry(int a) try if ( a == 1) a = a / (a - a); //division by zero if(a == 2) int c[] = {1}; c[42] = 99; //generate an out - of - bounds exception } catch(ArrayIndexOutOfBoundsException e) System.out.println("Array index out - of - bounds: " + e);

19 public static void main(String args[])
{ try int a = args.length; int b = 42 / a; //If no command - line args are present, the //following statement will generate a divide - by - zero exception System.out.println("a = " + a); // nested try bloack // If one command - line arg is used, then a divide - by - //zero exception will be generated by the following code. nesttry(a); // Calling Static Method } catch(ArithmeticException e) System.out.println("Divide by Zero: " + e);

20 throw You have only been catching exceptions that are thrown by the java run – time system. However, it is possible for your program to throw an exception explicitly. Using throw statement. Syntax: throw ThrowableInstance; Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Simple types, such as int or char, as well as non – Throwable classes, such as String and Object, cannot be used as exception. There are two ways you can obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator.

21 class Error_Handling { static void display() try throw new NullPointerException("Demo"); } catch(NullPointerException e) throw e; public static void main(String args[]) display(); System.out.println("Recaught : " + e);

22 throws If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. You do this by including a throws clause in the method’s declaration. A throws clause list the types of exceptions that a method might throw. This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile – time error will result. type method-name (parameter-list) throws exception-list { //body of method } Here, exception – list is comma – separated list of the exceptions that a method can throw

23 class Error_Handling { static void throwdemo() throws IllegalAccessException System.out.println("Inside throwdemo"); } public static void main(String args[]) try throwdemo(); catch(IllegalAccessException e) System.out.println("Caught " + e);

24 Using finally Statement
Java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statements. finally block can be used to handle any exception generated within a try block. It may be added immediately after the try block or after the last catch block. try { //statements } catch(Exception-Type-1 e) catch(Exception-Type-2 e) catch(Exception-Type-N e) finally try { //statements } finally

25 class Error_Handling { static void procA() try System.out.println("Inside ProcA"); throw new RuntimeException("demo"); } finally System.out.println("ProcA's finally"); static void procB() System.out.println("inside ProcB"); //return; System.out.println("ProcB's finally");

26 static void procC() { try System.out.println("inside ProcC"); } finally System.out.println("ProcC's finally"); public static void main(String args[]) procA(); catch(Exception e) System.out.println("Exception Caught"); System.out.println("Procedure A is Wind up"); procB(); procC(); } }

27 Java’s Built – in Exceptions
The standard package java.lang, Java defines several exception classes. A few have been used by the preceding examples. The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all java programs, most exceptions derived from RuntimeException are automatically available. Furthermore, they need not be included in any method’s throws list. In the language of Java, these are called unchecked exceptions because the compiler does not check to see if a method handles or throws these exceptions. The other exceptions defined by java.lang that must be included in a method’s throws list if that method can generate one of these exceptions and does not handle it itself. These are called checked exceptions.

28 Java’s Unchecked RuntimeException Subclasses
Meaning ArithmeticException Arithmetic error, such as divde – by – zero ArrayIndexOutOfBoundsException Array index is out – of – bounds. ArrayStoreException Assignment to an array element of an incompatible type. ClassCastException Invalid Cast. IllegalArgumentException Illegal argument used to invoke a method IllegalMonitoStateException Illegal Monitor Operation, such as waiting on an unlocked thread. IllegalStateException Environment or application is in incorrect state. IllegalThreadStateException Requested operation not compatible with current thread state. IndexOutOfBoundsException Some type of index is out – of – bounds NegativeArraySizeException Array Created with a negative size.

29 Exception Meaning NullPointerException Invalid use of a null reference. NumberFormatException Invalid conversion of a string to a numeric format. SecurityException Attempt to violate security StringIndexOutOfBounds Attempt to index outside the bounds of a string. UnsupportedOperationException An unsupported Operation was encountered.

30 Java’s Checked Exception Defined in java.lang
Meaning ClassNotFoundException Class not found CloneNotSupportedException Attempt to clone an object that does not implement the cloneable interface. IllegalAccessException Access to a class is denied InstantiationException Attempt to create an object of an abstract class or interface InterruptedException One thread has been interrupted by another thread. NoSuchFieldException A requested field does not exist. NoSuchMethodException A requested method does not exist.

31 throw new Throwable_subclass;
Throwing our own Exceptions There may be times when we would like to throw our own exceptions. We can do this by using the keyword throw as follows: throw new Throwable_subclass; Example: throw new ArithmeticException(); throw new NumberFormatException(); Note: Exception is subclass of Throwable and therefore MyException is a subclass of Throwable class. An object of a class that extends Throwable can be thrown and caught

32 import java.lang.Exception;
class MyException extends Exception { MyException(String message) super(message); } class Exception_Handling public static void main(String args[]) int x = 5, y = 1000; try float z = (float) x / (float) y; if (z < 0.01) throw new MyException("Number is too small");

33 catch(MyException e) { System.out.println("Caught my exception"); System.out.println(e.getMessage()); } finally System.out.println("I am always here");

34 Using Exceptions Exception handling provides a powerful mechanism for controlling complex programs that have many dynamic run – time characteristics. It is important to think of try, throw and catch as clean ways to handle errors and unusual boundary conditions in your program’s logic. If you are like most programmers, then you probably are used to returning an error code when a method fails. When you are programming in Java, you should break this habit. When a method can fail, have it throw an exception. This is a cleaner way to handle failure modes. One last point: Java’s Exception – handling statements should not be considered a general mechanism for nonlocal branching. If you do so, it will only confuse your code and make it hard to maintain.

35 THE CHARACTER STREAMS

36 The character Streams While the byte stream classes provide sufficient functionality to handle any type of I/O operation, they cannot work directly with Unicode characters. Since one of the main purposes of Java is to support the “write once, run anywhere” philosophy, it was necessary to include direct I/O support for characters. At the top of the character stream hierarchies are the Reader and Writer abstract classes. Reader Reader is an abstract class that defines java’s model of streaming character input. All of the methods in this class will throw an IOException on error conditions. Writer Writer is an abstract class that defines streaming character output . All of the methods in this class return a void value and throw an IOException in the case of errors.

37 The Methods Defined by Reader
Description abstract void close() Closes the input source. Further read attempts will generate an IOException. void mark(int numChars) Places a mark at the current point in the input stream that will remain valid until numChars characters are read boolean markSupported() Returns true if mark() / reset() are supported on this stream int read() Returns an integer representation of the next available character from the invoking input stream. -1 is returned when the end of the file is encountered. int read(char buffer[]) Attempts to read up to buffer.length characters into buffer and returns the actual number of character that were successfully read. -1 is returned when the end of the file encountered. abstract int read( char buffer[], int offset, int numChars) Attempts to read up to numChars characters into buffer starting at buffer[offset], returning the number of characters successfully read. -1 is returned when the end of the file is encountered.

38 Method Description boolean ready() Returns true if the next input request will not wait. Otherwise, it returns false. void reset() Resets the input pointer to the previously set mark long skip( long numChars) Skips over numChars characters of input , returning the number of characters actually skipped.

39 The Methods Defined by Writer
Description abstract void close() Closes the output stream. Further write attempts will generate an IOException. abstract void flush() Finalizes the output state so that any buffers are cleared. That is, it flushes the output buffers. void write (int ch) Writes a single character to the invoking output stream. Note that the parameter is an int, which allows you to call write with expressions without having to cast them back to char. void write(char buffer[]) Writes a complete array of characters to the invoking output stream. abstract void write( char buffer[], int numChars) Writes a subrange of numChars characters from the array buffer, beginning at buffer[offset] to the invoking output stream. void write(String str) Writes str to the invoking output stream. void write(String str, int offset, Writes a subrange of numChars characters from the array str, beginning at the specified offset.

40 FileReader The FileReader class creates a Reader that you can use to read the contents of a file. Its most commonly used constructors are shown here: FileReader (String filePath) FileReader (File filObj) Note: Either can throw a FileNotFoundException. Here, filePath is the full path name of a file and fileobj is a File object that describes the file.

41 import java.io.*; class FileReaderDemo { public static void main(String args[]) throws IOException FileReader fr = new FileReader("FileReaderDemo.java"); BufferedReader br = new BufferedReader(fr); String s; while((s = br.readLine()) != null) System.out.println(s); } fr.close();

42 FileWriter The FileWriter class creates a Writer that you can use to writet to a file. Its most commonly used constructors are shown here: FileWriter(String filePath) FileWriter(String filePath, boolean append) FileWriter(File fileObj) FileWriter(File fileObj, boolean append) They can throw an IOException. Here, filePath is the full path name of a file, and fileObj is a File Object that describes the file. If append is true, then output is appended to the end fo the file.

43 import java.io.*; class FileWriterDemo { public static void main(String args[]) try // Create file FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("Hello Java"); //Close the output stream out.close(); } catch (Exception e) //Catch exception if any System.err.println("Error: " + e.getMessage());

44 CharArrayReader CharArrayReader is an implementation of an input stram that uses a character array as the source. The class has two constructos, each of which requires a character array to provide the data source: CharArrayReader(char arrray[]) CharArrayReader(char array[],int start,int numChars) Here, array is the input source. The second constructor creates a Reader from a subset of your character array that begins with the character at the index specified by start and is numChars long.

45 import java.io.*; class CharArrayReaderDemo { public static void main(String args[]) throws IOException String tmp = "abcdefghijklmnopqrstuvwxyz"; int length = tmp.length(); char c[] = new char[length]; tmp.getChars(0, length, c, 0); CharArrayReader input1 = new CharArrayReader(c); CharArrayReader input2 = new CharArrayReader(c, 0, 5); int i; System.out.println("input1 is:"); while((i = input1.read()) != -1) System.out.print((char)i); } System.out.println(); System.out.println("input2 is:"); while((i = input2.read()) != -1)

46 CharArrayWriter(int numChars)
CharArrayWriter is an implementation of an output stram that uses a character array as the destination. The class has two constructos, each of which requires a character array to provide the data destination: CharArrayWriter() CharArrayWriter(int numChars) In the first form, a buffer with a default size is created. In the second, a buffer is created with a size equal to that specified by numChars. The buffer is held in the buf field of CharArrayWriter. The buffer size will be increased automatically, if needed. The number of characters held by the buffer is contained in the count field of CharArrayWriter. Both buf and count are protected fields.

47 import java.io.*; class CharArrayWriterDemo { public static void main(String args[]) throws IOException CharArrayWriter f = new CharArrayWriter(); String s = "This should end up in the array"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); f.write(buf); System.out.println("Buffer as a string"); System.out.println(f.toString()); System.out.println("Into array"); char c[] = f.toCharArray(); for (int i=0; i<c.length; i++) System.out.print(c[i]); } System.out.println("\nTo a FileWriter()"); FileWriter f2 = new FileWriter("test.txt"); f.writeTo(f2); f2.close(); System.out.println("Doing a reset"); f.reset(); for (int i=0; i<3; i++) f.write('X'); } }

48 BufferedReader BufferedReader improves performance by buffering input. It has two constructors: BufferedReader(Reader inputStream) BufferedReader(Reader inputStream,int bufsize) The first form creates a buffered character stream using a default buffer size. In the second, the size of the buffer is passed in bufsize.

49 import java.io.*; class BufferedReaderDemo { public static void main(String args[]) throws IOException String s = "This is a © copyright symbol " + "but this is &copy not.\n"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); CharArrayReader in = new CharArrayReader(buf); BufferedReader f = new BufferedReader(in); int c; while((c = f.read()) != -1) System.out.print((char)c); }

50 BufferedWriter A BufferedWriter is a Writer that adds a flush() method that can be used to ensure that data buffers are physically wirtten to the actual output stream. Using a BufferedWriter can increase performance by reducing the number of times data is actually physically written to the output stream. A BufferedWriter has these two constructors: BufferedWriter(Writer outputStream) BufferedWriter(Writer outputStream,int bufsize) The first form creates a buffered character stream using a default buffer size. In the second, the size of the buffer is passed in bufsize.

51 import java.io.*; class BufferedWriterDemo { public static void main(String args[]) throws Exception // Create a new instance of a BufferedWriter object using a StringWriter. StringWriter sw = new StringWriter(); BufferedWriter bw = new BufferedWriter(sw); // Write to the underlying StringWriter. String str = new String("This is the string being written."); // Print out the 6 characters. bw.write(str, 12, 6); bw.flush(); System.out.println(sw.getBuffer()); // Close the BufferedWriter object and the underlying StringWriter object. sw.close(); bw.close(); }

52 PushbackReader The PushbackReader class allows one or more characters to be returned to the input stream. This allows you to look ahead in the input stream. Here are its two constructors: PushbackReader(Reader inputStream) PushbackReader(Reader inputStream,int bufSize) The first form creates a buffered stream that allows one character to be pushed back. In the second, the size of the pushback buffer is passed in bufSize. PushbackReader provides unread(), which returns one or more characters to the invoking input stream. It has the three forms shown here: void unread(int ch) void unread(char buffer[]) void unread(char buffer[], int offset, int numChars) The first form pushes back the character passed in ch. This will be the next character returned by a subsequent call to read(). The second form returns the characters in buffer. The third form pushes back numChars characters beginning at offset from buffer. An IOException will be thrown if there is an attempt to return a character when the pushback buffer is full.

53 import java.io.*; class PushbackReaderDemo { public static void main(String args[]) throws IOException String s = "if (a == 4) a = 0 ; \\n"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); CharArrayReader in = new CharArrayReader(buf); PushbackReader f = new PushbackReader(in); int c; while ((c = f.read()) != -1) switch(c) case '=': if ((c = f.read()) == '=') System.out.print(".eq."); else System.out.print("<-"); f.unread(c); } break; default: System.out.print((char) c); } } }

54 PrintWriter PrintWriter is essentially a character – oriented version of PrintStream. It provides the formatted output methods print() and println(). PrintWriter has four constructors: PrintWriter(OutputStream outputStream) PrintWriter(OutputStream outputStream, boolean flushOnNewline) PrintWriter(Writer outputStream) PrintWriter(Writer outputStream, boolean flushOnNewline) Where flushOnNewline controls whether Java flushes the output stream every time println() is called. If flushOnNewline is true, flushing automatically takes place. If false, flushing is not automatic. The first and third constructors do not automatically flush.

55 import java.io.*; class PrintWriterDemo { public static void main(String args[]) PrintWriter pw = new PrintWriter(System.out, true); pw.println("This is a string"); int i = -7; pw.println(i); double d = 4.5e-7; pw.println(d); }

56 SERIALIZATION

57 Serialization Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to save the state of your program to a persistent storage area, such as a file. At a later time, you may restore these objects by using the process of deserialization. Serialization is also needed to implement Remote Method Invocation (RMI). RMI allows a Java object on one machine to invoke a method of a Java object on a different machine. An object may be supplied as an argument to that remote method. The sending machine serializes the object and transmits it. The receiving machine deserializes it.

58 void writeExternal(ObjectOutput outStream) throws IOException
Serializable Only an object that implements the Serializable interface can be saved and restored by the serialization facilities. The Serializable interface defines no members. It is simply used to indicate that a class may be serialized. If a class is serializable, all of its subclasses are also serializable. Variables that are declared as transient are not saved by the serialization facilities. Also static variable are not saved. Externalizable The Java facilities for serialization and deserialization have been designed so that much of the work to save and restore the state of an object occurs automatically. However, there are cases in which the programmer may need to have control over these processes. For example, it may be desirable to use compression or encryption techniques. The Externalizable interface is designed for these situations. The Externalizable interface defines these two methods: void readExternal(ObjectInput inStream) throws IOException, ClassNotFoundException void writeExternal(ObjectOutput outStream) throws IOException In these methods, inStream is the byte stream from which the object is to be read, and outStream is the byte stream to which the object is to be written.

59 ObjectOutput The ObjectOutput interface extends the DataOutput interface and support object Serialization. The following Methods is called the serialize an object. All of these methods will throw an IOException on error condition. Method Description void close() Closes the invoking stream. Further write attempts will generate an IOException. void flush() Finalizes the output state so that any buffers are cleared. That is, it flushes the output buffers. void wirte(byte buffer[]) Writes an array of bytes to the invoking stream. void write(byte buffer[], int offset, int numBytes) Writes a subrange of numBytes bytes from the array buffer, beginning at buffer[offset]. void write(int b) Writes a single byte to the invoking stream. The byte written is the low – order byte of b. Void writeObject(Object obj) Writes object obj to the invoking stream.

60 ObjectOutputStream(OutputStream outStream) throws IOException
The ObjectOutputStream class extends the OutputStream class and implements the ObjectOutput interface. It is responsible for writing objects to a stream. A constructor of this class is: ObjectOutputStream(OutputStream outStream) throws IOException The argument outStream is the output stream to which serialized objects will be written. Method Description void close() Closes the invoking stream. Further write attempts will generate an IOException. void flush() Finalizes the output state so that any buffers are cleared. That is, it flushes the output buffers. void wirte(byte buffer[]) Writes an array of bytes to the invoking stream. void write(byte buffer[], int offset, int numBytes) Writes a subrange of numBytes bytes from the array buffer, beginning at buffer[offset]. void write(int b) Writes a single byte to the invoking stream. The byte written is the low – order byte of b. void writeBoolean(boolean b) Writes boolean to the invoking stream.

61 Method Description void writeByte(int b) Writes a byte to the invoking stream. The byte written is the low – order byte of b. void writeBytes(String str) Writes the bytes representing str to the invoking stream. void writeChar(int c) Writes a char to the invoking stream. void writeChars(String str) Writes the characters in str to the invoking stream. void writeDouble(double d) Writes a double to the invoking stream. void writeFloat(float f) Writes a float to the invoking stream. void writeInt(int i) Writes a int to the invoking stream. void WriteShort(int i) Writes a short to the invoking stream. void writeLong(long i) Writes a long to the invoking stream. final void writeObject(Object obj) Writes a obj to the invoking stream.

62 ObjectInput The ObjectInput interface extends the DataInput interface and support object Serialization. The following Methods is called the serialize an object. All of these methods will throw an IOException on error condition. Method Description int available() Returns the number of bytes that are now available in the input buffer. void close() Closes the invoking stream. Further read attempts will generate an IOException. int read() Returns an integer representation of the next available byte of input. -1 is returned when the end of the file is encountered. int read(byte buffer[]) Attempts to read up to buffer length bytes into buffer, returning the number of bytes that were successfully read. -1 is returned when the end of the file is encountered. int read(byte buffer[], int offset, int numBytes) Attempts to read up to numBytes bytes into buffer starting at buffer[offset], returning the number of bytes that were successfully read. -1 is returned when the end of the file is encountered.

63 ObjectInputStream Method Description Object readObject()
Reads an object from the invoking stream. Long skip(long numBytes) Ignores(that is, skips) numBytes bytes in the invoking stream, returnign the number of bytes actually ignored. ObjectInputStream The ObjectInputStream class extends the InputStream class and implements the ObjectInput interface. It is responsible for Reading objects from a stream. A constructor of this class is: ObjectInputStream(InputStream inStream) throws IOException, StreamCorruptedException The argument inStream is the input stream from which serialized objects should be read.

64 Method Description int available() Returns the number of bytes that are now available in the input buffer. void close() Closes the invoking stream. Further read attempts will generate an IOException int read() Returns an integer representation of the next available byte of input. -1 is returned when the end of the file is encountered. int read(byte buffer[],int offset,int numBytes) Attempts to read up to numBytes bytes into buffer starting at buffer[offset], returning the number of bytes successfully read. -1 is returned when the end of the file is encountered. boolean readBoolena() Reads and returns boolean from the invoking stream. byte readByte() Reads and returns a byte from the invoking stream. char readChar() Reads and returns a char from the invoking stream. double readDouble() Reads and returns a double from the invoking stream. float readFloat() Reads and returns a float from the invoking stream. void readFully(byte buffer[]) Reads buffer length bytes into buffer. Returns only when all bytes have been read. void readFully(byte buffer[], int offset, int numBytes) Reads numBytes bytes into buffer starting at buffer[offset]. Returns only when numBytes have been read.

65 Method Description int readInt() Reads and returns an int from the invoking stream. long readLong() Reads and returns a long from the invoking stream. final Object readObject() Reads and returns an object from the invoking stream. short readShort() Reads and returns a short from the invoking stream. int readUnsignedByte() Reads and returns an unsigned byte from the invoking stream. int readUnsignedShort() Reads an unsigned short from the invoking stream.

66 import java.io.*; class SerializationDemo { public static void main(String arg[]) //Object Serialization try MyClass object1 = new MyClass("Hello", -7,2.7e10); System.out.println("Object1: " + object1); FileOutputStream fos = new FileOutputStream("serial"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object1); oos.flush(); oos.close(); } catch(Exception e) System.out.println("Exception during serialization: " + e); System.exit(0);

67 //Object Deserialization
try { MyClass object2; FileInputStream fis = new FileInputStream("serial"); ObjectInputStream ois = new ObjectInputStream(fis); object2 = (MyClass)ois.readObject(); ois.close(); System.out.println("object2: " + object2); } catch(Exception e) System.out.println("Exception during deserialization: " + e); System.exit(0); class MyClass implements Serializable String s; int i; double d; public MyClass(String s,int i,double d) this.s = s; this.i = i; this.d = d; public String toString() return "S=" + s + "; i=" + i + "; d=" + d; } }

68 Constructor Calling Conversion Action Method Calling Conversion Action
Converting Primitive Numbers to Object Numbers using Constructor Methods Constructor Calling Conversion Action Integer intval = new Integer(i) Primitive integer to Integer object Float floatval = new Float(f) Primitive float to Float object Double doubleval = new Double(d) Primitive double to Double object Long longval = new Long(l) Primitive long to Long object Converting Object Numbers to Primitive Numbers using typeValue() method Method Calling Conversion Action int i = IntVal.intValue() Object to Primitive integer float f = FloatVal.floatValue() Object to Primitive float long l = LongVal.longValue() Object to Primitive long double d = DoubleVal.doubleValue() Object to Primitive double

69 Constructor Calling Conversion Action Method Calling Conversion Action
Converting Numbers to Strings Using a String( ) Method Constructor Calling Conversion Action str = Integer.toString(i) Primitive Integer to string str = Float.toString(f) Primitive float to string str = Double.toString(d) Primitive double to string str = Long.toString(l) Primitive long to string Converting String Objects to Numeric Object Using the Static Method ValueOf() Method Calling Conversion Action DoubleVal = Double.ValueOf(str) Converts String to Double object FloatVal = Float.ValueOf(str) Converts String to Float object IntVal = Integer.ValueOf(str) Converts string to Integer object LongVal = Long.Valueof(str) Converts string to Long object

70 Constructor Calling Conversion Action
Converting Numberic Strings to Primitive Numbers Using Parsing Methods Constructor Calling Conversion Action int i = Integer.parseInt(str) Converts String to Primitive Integer long l = Long.parseLong(str) Converts String to Primitive long

71 THREAD MODEL

72 INTRODUCTION Those who are familiar with the modern operation systems such as windows 95 may recognize that they can execute several programs simultaneously. This ability is known as multitasking. In System’s terminology, it is called multithreading. Multithreading is a conceptual programming paradigm where a program is divided into two or more subprograms, which can be implemented at the same time in parallel. For example, one subprogram can display an animation on the screen while another may build the next animation to be displayed. This something similar to dividing a task into subtasks and assigning them to different people for execution independently and simultaneously. In most of our computers, we have only a single processor and therefore, in reality, the processor is doing only one thing at a time. However, the processor switches between the processes so fast that it appears to human beings that all of them are being done simultaneously. Java programs contain only a single sequential flow of control. This is what happens when we execute a normal program. The program begins, runs through a sequence of executions, and finally ends. At any given point of time, there is only one statement under execution. A thread is similar to a program that has a single flow of control. It has a beginning, a body and an end, and executes commands sequentially. In fact, all main programs in our earlier examples can be called single – threaded programs. A unique property of Java is its support for multithreading. That is, java enables us to use multiple flows of control in developing programs. Each flow of control may be thought of as in separate tiny program known as thread that runs in parallel to others.

73 SINGLE THREADED PROGRAM
class ABC { } Beginning Single – threaded body of execution End

74 MULTITHREADED PROGRAM
Main Thread _______________ Main method module start start start _______________ _______________ _______________ switching switching Thread A Thread B Thread C

75 7. A program that contains multiple flows of control is known as multithreaded program.
8. The above fig. is a java program with four threads, one main and three others. The main thread is actually the main method module, which is designed to create and start the other three threads, namely A, B and C 9. Once initiated by the main thread, the threads A, B and C run concurrently and share the resources jointly. The ability of a language to support multithreads is referred to as concurrency. 10. Threads in java are subprograms of main application program and share the same memory space, they are known as lightweight threads or lightweight processes.. Note: It is important to remember that ‘threads running in parallel’ does not really mean that they actually run at the same time. Since all the threads are running on a single processor, the flow of execution is shared between the threads.

76 1. Creating threads in Java is simple.
2. Threads are implemented in the form of objects that contain a method called run(). 3. The run() method is the heart and soul of any thread. It makes up the entire body of a thread and is the only method in which the thread’s behaviour can be implemented. 4. A typical run() would appear as follows: public void run() { } Statements for implementing thread

77 5. The run() method should be invoked by an object for the concerned thread.
6. This can be achieved by creating the thread and initiating it with the help of another thread method called start(). 7. A new thread can be created in two ways. a. By creating a thread class: Define a class that extends Thread class and override its run() method with the code required by the thread. b. By converting a class to a thread: Define a class that implements Runnable interface. The Runnable interface has only one method, run(), that is to be defined in the method with the code to be executed by the thread.

78 EXTENDING THE THREAD CLASS
We can make our class runnable as a thread by extending the class java.lang.Thread. This gives us access to all the thread methods directly. It includes the following steps: 1. Declare the class as extending the Thread class 2. Implement the run() method that is responsible for executing the sequence of code that the thread will execute. 3. Create a thread object and call the start() method to initiate the thread execution. 1. Declaring the class class MyThread extends Thread { }

79 2. Implementing the run() Method
public void run() { } Thread Code Here 3. Starting New Thread MyThread aThread = new MyThread(); aThread.start(); //Invoke run() method

80 Creating threads using the thread class
class A extends Thread { public void run() for(int i = 1;i <= 5;i++) System.out.println("\tFrom Thread A : i = " + i); } System.out.println("Exit from A "); class B extends Thread for(int j = 1;j <= 5;j++) System.out.println("\tFrom Thread B : j = " + j); System.out.println("Exit from B ");

81 class C extends Thread { public void run() for(int k = 1;k <= 5;k++) System.out.println("\tFrom Thread C : k = " + k); } System.out.println("Exit from C "); class ThreadTest public static void main(String args[]) A a = new A(); B b = new B(); C c = new C(); a.start(); b.start(); c.start();

82 IMPLEMENTING THE ‘RUNNABLE’ INTERFACE
The Runnable interface declares the run() method that is required for implementing threads in our programs. To do this, we must perform the steps listed below: 1. Declare the class an implementing the Runnable interface. 2. Implementing the run() method. 3. Create a thread by defining an object that is instanitiated form the “runnable” class as the target of the thread. 4. Call the thread’s start() method to run the thread.

83 import java.lang.Thread;
class X implements Runnable { public void run() for(int i = 1;i <= 5;i++) System.out.println("\tFrom Thread A : i = " + i); } System.out.println("Exit from A "); class Y implements Runnable for(int j = 1;j <= 5;j++) System.out.println("\tFrom Thread B : j = " + j); System.out.println("Exit from B ");

84 class Z implements Runnable
{ public void run() for(int k = 1;k <= 5;k++) System.out.println("\tFrom Thread C : k = " + k); } System.out.println("Exit from C "); class RunnableTest public static void main(String args[]) X runnableX = new X(); Thread threadX = new Thread(runnableX); Y runnableY = new Y(); Thread threadY = new Thread(runnableY); Z runnableZ = new Z(); Thread threadZ = new Thread(runnableZ); threadX.start(); threadY.start(); threadZ.start();

85 LIFE CYCLE OF A THREAD During the life time of a thread, there are many states it can enter. They include Newborn state Runnable state Running state Blocked state Dead state

86 State Transition diagram of a Thread
Newborn New Thread start stop Active Thread stop Runnable Dead Running yield Killed Thread suspend sleep wait resume notify stop Blocked Idle Thread (Not Runnable)

87 Newborn State When we create a thread object, the thread is born and is said to be in newborn state. The thread is not yet scheduled for running. At this state, we can do only one of the following things with it: 1. schedule it for funning using start() method. 2. kill it using stop() method. Newborn start stop Runnable state Dead State

88 Runnable State The runnable state means that the thread is ready for execution and is waiting for the availability of processor. That is, the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority, then they are given time slots for execution in round robin fashion. i.e., first – come, first – serve manner. The thread that relinquishes contol joins the queue at the end and again waits for its turn. This process of assigning time to threads is known as time – slicing. However, if we want a thread to relinquish control to another thread of equal priority before its turn comes, we can do so by using the yield() method. Yield() Running Thread

89 Running State Running means that the processor has given its time to the thread for its execution. The thread runs until it relinquishes control on its own or it is preempted by a higher priority thread. A running thread may relinquish its control in one of the following situations: 1. suspend() and resume() It has been suspended using suspend() method. A suspended thread can be revived by using the resume() method. This approach is useful when we want to suspend a thread for some time due to certain reason, bur do not want to kill it suspend() resume() Suspended Running Runnable

90 2. sleep() It has been made to sleep, We can put a thread to sleep for a specified time period using the method sleep (time) where time is in milliseconds. This means that the thread is out of the queue during this time period. The thread re – enters the runnable state as soon as this time period is elapsed. sleep (t) after t Sleeping Running Runnable

91 Blocked State 3. wait() and notify()
It has been told to wait until some event occurs. This is done using the wait() method. The thread can be scheduled to run again using the notify() method. wait() notify () Running Runnable Waiting Blocked State A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping, or waiting in order to satisfy certain requirements. A blocked thread is considered “not runnable” but not dead therefore fully qualified to run again.

92 Dead State Every thread has life cycle. A running thread ends its life when it has completed executing its run() method. It is a natural death. However, we can kill it by sending the stop message to it at any state thus causing a premature death to it. A thread can be killed as soon it is born, or while it is running, or even when it is in “not runnable” (blocked) condition.

93 import java.lang.Thread;
class A extends Thread { public void run() for(int i = 1;i <= 5;i++) if(i == 1) yield(); System.out.println("\tFrom Thread A : i = " + i); } System.out.println("Exit from A "); class B extends Thread for(int j = 1;j <= 5;j++) System.out.println("\tFrom Thread B : j = " + j); if(j==3) stop(); System.out.println("Exit from B ");

94 class C extends Thread { public void run() for(int k = 1;k <= 5;k++) System.out.println("\tFrom Thread C : k = " + k); if(k==1) try sleep(1000); } catch(Exception e) System.out.println("Exit from C "); class ThreadTest_1 public static void main(String args[]) A a = new A(); B b = new B(); C c = new C(); System.out.println("Start Thread A"); a.start(); System.out.println("Start Thread B"); b.start(); System.out.println("Start Thread C"); c.start(); System.out.println("End of main Thread");

95 final boolean isAlive() final void join() throws InterruptedException
Using isAlive() and join() isAlive() Two ways exist to determine whether a thread has finished. First, you can call isAlive() on the thread. The method is defined by Thread, and its general form is shown here: final boolean isAlive() The isAlive() method returns true if the thread upon which it is called is still running. It returns false otherwise. join While isAlive() is occasinally useful, the method that you will more commonly use to wait for a thread to finish is called join(), shown here: final void join() throws InterruptedException This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it.

96 class A extends Thread { public void run() for(int i = 1;i <= 5;i++) System.out.println("\tFrom Thread A : i = " + i); } System.out.println("Exit from A "); class B extends Thread for(int j = 1;j <= 5;j++) System.out.println("\tFrom Thread B : j = " + j); System.out.println("Exit from B ");

97 class C extends Thread { public void run() for(int k = 1;k <= 5;k++) System.out.println("\tFrom Thread C : k = " + k); } System.out.println("Exit from C "); class ThreadTest public static void main(String args[]) A a = new A(); B b = new B(); C c = new C();

98 System.out.println("Start Thread A");
a.start(); System.out.println("Start Thread B"); b.start(); System.out.println("Start Thread C"); c.start(); System.out.println("End of main Thread"); System.out.println("Thread One is Alive: " + a.isAlive()); System.out.println("Thread two is Alive: " + b.isAlive()); System.out.println("Thread three is Alive: " + c.isAlive()); try { a.join(); b.join(); c.join(); } catch(InterruptedException e) System.out.println("Main Thread Interrupted");

99 ThreadName.setPriority(intNumber);
THREAD PRIORITY In java, each thread is assigned a prioriy, which affects the order in which it is scheduled for running. The threads of the same priority are given equal treatment by the Java scheduler and, therefore, they share the processor of a first – come, first – serve basis. ThreadName.setPriority(intNumber); The intNumber is an integer value to which the thread’s priority is set. The Thread class defines several priority constants: MIN_PRIORITY = 1 NORM_PRIORITY = 5 MAX_PRIORITY = 10 The intNumber may assume one of these constants or any value between 1 and 10. Note that the default setting is NORM_PRIORITY

100 import java.lang.Thread;
class A extends Thread { public void run() for(int i = 1;i <= 5;i++) System.out.println("\tFrom Thread A : i = " + i); } System.out.println("Exit from A "); class B extends Thread for(int j = 1;j <= 5;j++) System.out.println("\tFrom Thread B : j = " + j); System.out.println("Exit from B ");

101 class C extends Thread { public void run() for(int k = 1;k <= 5;k++) System.out.println("\tFrom Thread C : k = " + k); } System.out.println("Exit from C "); class ThreadPriority public static void main(String args[]) A a = new A(); B b = new B(); C c = new C(); c.setPriority(5); //c.setPriority(NORM_PRIORITY); b.setPriority(10); //b.setPriority(MAX_PRIORITY); a.setPriority(1); //a.setPriority(MIN_PRIORITY); a.start(); b.start(); c.start();


Download ppt "EXCEPTION HANDLING OR ERROR HANDLING."

Similar presentations


Ads by Google