Chapter 4
Chapter 4
• That method may choose to handle the exception itself, or pass it on.
Either way, at some point, the exception is caught and processed.
3
Exception handling(cont’d…)
6
Exception Types
conditions that user programs should catch. This is also the class that
• The other branch is topped by Error, which defines exceptions that are
not expected to be caught under normal circumstances by your
program.
• All other exceptions except errors and runtime exception are known as checked
exceptions, meaning that the compiler forces the programmer to check and deal
with them.
• In most cases, unchecked exceptions reflect programming logic errors that are
unrecoverable.
• For example, an IndexOutOfBoundsException is thrown if you access an element in an
array outside the bounds of the array. These are logic errors that should be corrected in the
program.
9
Java’s Unchecked RuntimeException Subclasses
10
Java’s Checked Exceptions
11
Comparing Checked and unchecked Exception
12
Checked and unchecked Exception(cont’d…)
13
Uncaught Exceptions
• When the Java run-time system detects the attempt to divide by zero, it constructs a
new exception object and then throws this exception.
• This causes the execution of DivByZero to stop, because once an exception has been
thrown, it must be caught by an exception handler and dealt with immediately.
• In the above 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.
• Any exception that is not caught by your program will ultimately be processed by the
default handler.
15
Uncaught Exceptions(cont’d…)
stack trace from the point at which the exception occurred, and
16
Using try and catch
18
Multiple catch Clauses
• In some cases, more than one exception could be raised by a single piece
of code.
• To handle such like situation, you can specify two or more catch clauses,
each catching a different type of exception.
• After one catch statement executes, the others are bypassed, and execution
continues after the try / catch block.
19
Flowchart of Multi-catch Block
20
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.
21
Chapter 5: packages
• import java.io.*;
23
Defining a Package
• To create a package simply include a package command as the first statement
• Any classes declared within that file will belong to the specified package.
• The package statement defines a name space in which classes are stored.
• If you omit the package statement, the class names are put into the default
package.
• While the default package is fine for short, sample programs, it is inadequate
for real applications. Most of the time, you will define a package for your code.
24
Defining a Package…
• This is the general form to create package is:
package pkg;
• Here, pkg is the name of the package. For example, the following
statement creates a package called MyPackage:
• package MyPackage;
• For example, the .class files for any classes you declare to be part of
MyPackage must be stored in a directory called MyPackage.
25
Defining a Package…
• The package statement simply specifies to which package the classes
defined in a file belong. It does not exclude other classes in other files
from being part of that same package.
• To do so, simply separate each package name from the one above it
by use of a period.
• Java run-time system where packages that you create are/is exist
• First, by default, the Java run-time system uses the current working directory as
its starting point. Thus, if your package is in a subdirectory of the current
directory, it will be found.
• Second, you can specify a directory path or paths by setting the CLASSPATH
environmental variable.
• Third, you can use the - classpath option with java and javac to specify the path
to your classes.
27
Access Modifier
Specifies the scope of the data members, class and methods.
• private members of the class are available with in the class only.
• The scope of private members of the class is “CLASS
SCOPE”.
28
Access Modifier…
29
Access Modifier…
30