Java ExceptionIO
Java ExceptionIO
Java ExceptionIO
DEVELOPMENT PROGRAM
Java 8 & File I/O & Exception
OUTLINE
• Exceptions
• Java 8
• Java 1/O
• Serialization
EXCEPTION IN JAVA
EXCEPTION HIERARCHY
EXCEPTION
• An exception is an unwanted or unexpected event, which occurs during the execution of
a program i.e at run time, that disrupts the normal flow of the program’s instructions.
• Trying to access the 11th element of an array when the array contains of only 10
elements. ( ArrayIndexOutOfBoundsException)
• Division by zero ( ArithmeticException)
• Accessing a file which is not existing ( FileNotFoundException)
• Failure of I/O operations ( IOException)
ERROR VS. EXCEPTION
• Error — An Error indicates serious problem that a reasonable
application should not try to catch.
• IOException, SQLException
• Unchecked exception
• ArrayIndexOutOfBoundsException, NullPointerException
EXCEPTION HANDLING
• try-catch
• throw
• throws
• finally
TRY-CATCH
• try/catch block can be placed within any method that you feel
can throw exceptions
• catch block is used to catch any exception raised from the try
block
• Use throw keyword to throw exception whenever you want to raise the
exception
…….
Default and Static Methods
• Static methods belong only to the interface and not classes that
implement the interface
• Lambda Expression
• Lambda expressions basically express instances
of functional interfaces
• lambda expressions implement the only
abstract function and therefore implement
functional interfaces
Lambda Expression
• Syntax
• parameter -> expression body
• characteristics
• Optional type declaration
• Optional parentheses around parameter
• Optional curly braces
• Optional return keyword
Functional Interface & Lambda
Expression
STREAM API
https://docs.oracle.com/javase/8/docs/api/?java/util/stream/Stream.html
Intermediate Operations VS
Terminal Operations
• Main Difference:
• Intermediate operations return a Stream as a result and
terminal operations return non-stream values like primitive
or object or collection or may not return anything.
• Intermediate operations are lazily loaded. When you call
intermediate operations, they are actually not executed.
They are just stored in the memory and executed when the
terminal operation is called on the stream.
STREAM API EXAMPLE
Consider Following Question:
return a String List, which contains the names of the employees whose
● ID >1
● Wage >= 40
And the result should be in uppercase and in Alphabetical order
For Loop Solution
Stream Solution
Optional Class
static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if
non-null, otherwise returns an empty Optional.
void ifPresent(Consumer<? super T> consumer) If a value is present, it invokes the specified consumer
with the value, otherwise does nothing.
T orElseGet(Supplier<? extends T> other) Returns the value if present, otherwise invokes other
and returns the result of that invocation.
Optional<T> filter(Predicate<? super <T> If a value is present and the value matches a given
predicate) predicate, it returns an Optional describing the value,
otherwise returns an empty Optional.
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
Optional Class Example
• Think about the following code:
Optional Class Example
Optional Class Example
JAVA I/O
• Java I/O (Input and Output) is used to process
the input and produce the output (read and
write data). Most application need to process
some data and produce some output based on
the input.
• In Java, stream can either be byte based (reading and writing bytes)
or character based (reading and writing character)
• Stream does not have concept of an index (like array) , nor can we
typically move forward and backward (like array)
• It is just a flow of data.
• Some classes like PushbackInputStream allows you to push data back
to stream again, but the functionality is limited.You can not traverse
the data at will.
STREAMS IN JAVA
In java, 3 streams are created for us automatically. All these streams are
attached with console.
• System.out: System.out.println(“Hello”);
• System.err: System.err.println(“Stop”);
• System.in
• System.in.read();
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/i
o/InputStream.html
INPUTSTREAM
• Most of the methods in this class will throw an IOException when an I/O error
occurs
• Some methods:
• Read() method returns actual number of bytes that were successfully read or -1 if end of
the file is reached.
OUTPUTSTREAM
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/ja
va/io/OutputStream.html
STREAMS
• OutputStream is an abstract class that defines streaming byte output.
• Most of the methods in this class return void and throw an IOException in the case of
I/O errors.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/Reader.html
WRITER IN JAVA
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/Writer.html
READER & WRITER
• File deals directly with files and the file system. That is, the File class does not
specify how information is retrieved from or stored in files; it describes the
properties of a file itself.
Objects are of Object type. It’s developers job to cast them to a specific type.
JVM will load the class and restore the objects without calling their constructors.
Constructors will be called only for those of non-serializable classes.
Transient variables are given null or default values (0, false, etc.) for primitives.
WHAT CAN HURT
DESERIALIZATION
• Deleting an instance variable
• Changing the declared type of an instance variable
• Changing a non-transient instance variable to
transient
• Moving a class up or down the inheritance hierarchy
• Removing “implements Serializable
• Changing an instance variable to static
SERIALVERSIONUID
• Built-in static variable called serialVersionUID
• If you change your class, JVM will think the class is
different now and will throw exception
• You can manually write the serialVersionUID to signal the
JVM it’s OK to keep deserializing
• In the real world, very few developer does this. Usually
new classes are created and new tables are created.
QUESTIONS?