Vous êtes sur la page 1sur 28

Core Java Concepts:-

The term exception is shorthand for the phrase


"exceptional event.
Definition: An exception is an event, which occurs
during the execution of a program, that disrupts the
normal flow of the program's instructions.

checked exception
These are exceptional conditions that a well-written
application should anticipate and recover from.

Runtime-exception
These are exceptional conditions that are internal to the
application, and that the application usually cannot anticipate
or recover from. These usually
programming bugs, such as
logic errors or improper use of an API.

Error
These are exceptional conditions that are external to
the application, and that the application usually cannot
anticipate or recover from. For example, suppose that an
application successfully opens a file for input, but is unable to
read the file because of a hardware or system malfunction

try
The first step in constructing an exception handler is to
enclose the code that might throw an exception within a
try block.

catch
You associate exception handlers with a try block by
providing one or more catch blocks directly after the
try
block. No code can be between the end of the try
block and
the beginning of the first catch block.
try {
} catch (ExceptionType name) {
} catch (ExceptionType name) {
}

finally
The finally block always executes when the try block
This ensures that the finally block is executed even if an
unexpected exception occurs.

exits.

throws
The throws keyword in java programming language is
applicable to a method to indicate that the method raises
particular type of exception while being processed.

throw
All methods use the throw statement to throw an exception. The
throw statement requires a single argument: a throwable
object.
Throwable objects are instances of any subclass of the Throwable
class. Here's an example of a throw statement.

throw someThrowableObject;

Java Exception Hierarchy

import java.io.*;
public class exceptionHandle{
public static void main(String[] args) throws Exception{
try
{
int a,b;
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
a = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
}
catch(NumberFormatException ex){
System.out.println(ex.getMessage() + " is not a numeric value.");
System.exit(0);
}
}
}

Question: Is the following code legal?


try {
}
finally {
}
Question: Is there anything wrong with this exception
handler as written? Will this code compile?
try {
} catch (Exception e) {
} catch (ArithmeticException a) {
}

What is Input Stream?????


A program uses an input stream to read data from a
source, one item at a time

What is Output Stream?????


A program uses an output stream to write data to a
destination, one item at time:

import java.io.*;
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}if (out != null) {
out.close();
}
}}}

A Frame is a subclass of window. It is window


with a title and resizing corners

FlowLayout
BorderLayout
GridLayout
GridBagLayout
CardLayout

Swing is the primary Java GUI Widget Toolkit. It is part


of Sun Microsystems, Java Foundation classes (JFC)
an API for providing a GUI for Java programs.

Running part of a program is known as


Thread.
In Java Threads can be created into two
ways:
By implementing Runnable Interface
By extending super class Thread

New: A new thread begins its life cycle in


the new state.
Runnable: After a newly born thread is
started, the thread becomes runnable.
Waiting: Sometimes a thread transitions
to the waiting state while the thread waits
for another thread to perform a task.
Timed waiting: A runnable thread can
enter the timed waiting state for a
specified interval of time.
Terminated: A runnable thread enters
the terminated state when it completes its
task or otherwise terminates.

public class HelloRunnable implements Runnable


{
public void run()
{
System.out.println("Hello from a thread!");
}
public static void main(String args[])
{
(new Thread(new HelloRunnable())).start();
}
}

Question: What is thread?


Question: What is Multitasking?
Question: How can we create a thread?

Vous aimerez peut-être aussi