28
Core Java Concepts:-

Core Java Tutorials - Exception Handling and Java Frames

Embed Size (px)

Citation preview

Page 1: Core Java Tutorials - Exception Handling and Java Frames

Core Java Concepts:-

Page 2: Core Java Tutorials - Exception Handling and Java Frames

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.

Page 3: Core Java Tutorials - Exception Handling and Java Frames

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

Runtime-exceptionThese 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.

ErrorThese 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

Page 4: Core Java Tutorials - Exception Handling and Java Frames

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) {}

Page 5: Core Java Tutorials - Exception Handling and Java Frames

finallyThe finally block always executes when the try block

exits. This ensures that the finally block is executed even if an unexpected exception occurs.

throwsThe throws keyword in java programming language is applicable to a method to indicate that the method

raises particular type of exception while being processed.

throwAll 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;

Page 6: Core Java Tutorials - Exception Handling and Java Frames

Java Exception Hierarchy

Page 7: Core Java Tutorials - Exception Handling and Java Frames

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); } }}

Page 8: Core Java Tutorials - Exception Handling and Java Frames
Page 9: Core Java Tutorials - Exception Handling and Java Frames

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) { }

Page 10: Core Java Tutorials - Exception Handling and Java Frames
Page 11: Core Java Tutorials - Exception Handling and Java Frames

What is Input Stream?????

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

Page 12: Core Java Tutorials - Exception Handling and Java Frames

What is Output Stream?????A program uses an output stream to write data to a destination, one item at time:

Page 13: Core Java Tutorials - Exception Handling and Java Frames
Page 14: Core Java Tutorials - Exception Handling and Java Frames
Page 15: Core Java Tutorials - Exception Handling and Java Frames

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(); } }}}

Page 16: Core Java Tutorials - Exception Handling and Java Frames
Page 17: Core Java Tutorials - Exception Handling and Java Frames
Page 18: Core Java Tutorials - Exception Handling and Java Frames

A Frame is a subclass of window. It is window with a title and resizing corners

Page 19: Core Java Tutorials - Exception Handling and Java Frames

FlowLayout BorderLayout GridLayout GridBagLayout CardLayout

Page 20: Core Java Tutorials - Exception Handling and Java Frames

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.

Page 21: Core Java Tutorials - Exception Handling and Java Frames
Page 22: Core Java Tutorials - Exception Handling and Java Frames

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

Page 23: Core Java Tutorials - Exception Handling and Java Frames

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.

Page 24: Core Java Tutorials - Exception Handling and Java Frames

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(); }

}

Page 25: Core Java Tutorials - Exception Handling and Java Frames
Page 26: Core Java Tutorials - Exception Handling and Java Frames

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

Page 27: Core Java Tutorials - Exception Handling and Java Frames
Page 28: Core Java Tutorials - Exception Handling and Java Frames