0% found this document useful (0 votes)
7 views40 pages

Java M4

Uploaded by

dineshnceh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Java M4

Uploaded by

dineshnceh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

MODULE -4

Packages

A set of classes and interfaces grouped


together are known as Packages in JAVA

Packages are containers for classes.

They are used to keep the class namespace compartmentalized.

Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

Tuesday 26 November 2024 . 1


MODULE -4

Defining a Package

To create a package is quite easy:


simply include a package command as the first statement in a Java source file.

Any classes declared within that file will belong to the specified package.

The package statement defines a namespace in which classes are stored.

If you omit the package statement, the class names are put into the default package,
which has no name.

Tuesday 26 November 2024 . 2


MODULE -4

Tuesday 26 November 2024 . 3


MODULE -4

Tuesday 26 November 2024 . 4


MODULE -4

You can create a hierarchy of packages.

To do so, simply separate each package name from the one above it by use of a period.

The general form of a multileveled package statement is shown here:

Tuesday 26 November 2024 . 5


MODULE -4

Packages and Member Access


 Classes and packages are both means of encapsulating and containing the
name space and scope of variables and methods.
 Packages act as containers for classes and other subordinate packages.
 Classes act as containers for data and code.
 The class is Java’s smallest unit of abstraction. Because of the interplay
between classes and packages, Java addresses four categories of visibility for
class members:

Tuesday 26 November 2024 . 6


MODULE -4

Tuesday 26 November 2024 . 7


MODULE -4

This is file Derived.java:

Tuesday 26 November 2024 . 8


MODULE -4

Tuesday 26 November 2024 . 9


MODULE -4

Tuesday 26 November 2024 . 10


MODULE -4

This is file OtherPackage.java:

Tuesday 26 November 2024 . 11


MODULE -4

Tuesday 26 November 2024 . 12


MODULE -4

The test file for p2 is shown next:

Tuesday 26 November 2024 . 13


MODULE -4

Tuesday 26 November 2024 . 14


MODULE -4

Exception Handling

An exception is an abnormal condition that arises in a code sequence at run time.

In other words, an exception is a run-time error.

In computer languages that do not support exception handling, errors must be checked
and handled manually—typically through the use of error codes, and so on.

Tuesday 26 November 2024 . 15


MODULE -4

Exception-Handling Fundamentals

A Java exception is an object that describes an exceptional (that is,


error)condition that has occurred in a piece of code.

When an exceptional condition arises, an object representing that exception


is created and thrown in the method that caused the error. That method
may choose to handle the exception itself, or pass it on. Either way, at some
point, the exception is caught and processed.

Exceptions can be generated by the Java run-time system, or they can be


manually generated by your code.

Tuesday 26 November 2024 . 16


MODULE -4

Java exception handling is managed via five


keywords:
try,
catch,
throw,
throws,
finally.

Tuesday 26 November 2024 . 17


MODULE -4

general form of an exception-handling block:

Tuesday 26 November 2024 . 18


MODULE -4

Exception Types
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the
exception class hierarchy.

Tuesday 26 November 2024 . 19


MODULE -4

Tuesday 26 November 2024 . 20


MODULE -4

Tuesday 26 November 2024 . 21


MODULE -4

Using try and catch


To illustrate how easily this can be done, the following program includes a try
block and a catch clause that processes the ArithmeticException generated by
the division-by-zero error:

Tuesday 26 November 2024 . 22


MODULE -4

Tuesday 26 November 2024 . 23


MODULE -4

Multiple catch Clauses


In some cases, more than one exception could be raised by a single piece of
code.

To handle this type of situation, you can specify two or more catch
clauses, each catching a different type of exception.

When an exception is thrown, each catch statement is inspected in order, and


the first one whose type matches that of the exception is executed.

After one catch statement executes,the others are bypassed, and execution
continues after the try / catch block.

Tuesday 26 November 2024 . 24


MODULE -4

Tuesday 26 November 2024 . 25


MODULE -4

Nested try Statements

The try statement can be nested.

That is, a try statement can be inside the block of another try.

Each time a try statement is entered, the context of that exception is pushed on the stack.

If an inner try statement does not have a catch handler for a particular exception, the stack is unwound
and the next try statement’s catch handlers are inspected for a match. This continues until one of the
catch statements succeeds, or until all of the nested try statements are exhausted.

If no catch statement matches, then the Java run-time system will handle the exception.

Tuesday 26 November 2024 . 26


MODULE -4

Tuesday 26 November 2024 . 27


MODULE -4

Tuesday 26 November 2024 . 28


MODULE -4

So far, you have only been catching exceptions that are thrown by the Java runtime
system.

However, it is possible for your program to throw an exception explicitly, using the throw
statement.

The general form of throw is shown here:

Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable.

Primitive types, such as int or char, as well as non-Throwable classes, such as String and
Object, cannot be used as exceptions.

Tuesday 26 November 2024 . 29


MODULE -4

There are two ways you can obtain a Throwable object: using a parameter in a
catch clause or creating one with the new operator.

Tuesday 26 November 2024 . 30


MODULE -4

The flow of execution stops immediately after the throw statement;


any subsequent statements are not executed.

The nearest enclosing try block is inspected to see if it has a catch


statement that matches the type of exception.
If it does find a match, control is transferred to that statement. If not,
then the next enclosing try statement is inspected, and so on.

If no matching catch is found, then the default exception handler


halts the program and prints the stack trace.

Tuesday 26 November 2024 . 31


MODULE -4

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.

A throws clause lists 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.

Tuesday 26 November 2024 . 32


MODULE -4

Tuesday 26 November 2024 . 33


MODULE -4

Tuesday 26 November 2024 . 34


MODULE -4

Java’s Built-in Exceptions


Inside the standard package java.lang, Java defines several exception classes.

Tuesday 26 November 2024 . 35


MODULE -4

Tuesday 26 November 2024 . 36


MODULE -4

Tuesday 26 November 2024 . 37


MODULE -4

Tuesday 26 November 2024 . 38


MODULE -4

Creating Your Own Exception


Subclasses Although Java’s built-in exceptions handle most common
errors, you will probably want to create your own exception
types to handle situations specific to
your applications.

This is quite easy to do: just define a subclass of


Exception (which is, of course, a subclass of
Throwable).
The Exception class does not define any methods of
its own. It does, of course, inherit those methods
provided by Throwable. Thus, all exceptions,
including those that you create, have the methods defined
by Throwable available to them.

Tuesday 26 November 2024 . 39


MODULE -4

Tuesday 26 November 2024 . 40

You might also like