MZ Chapter - 4 and 5 - Exception Handling and Packages Oop
MZ Chapter - 4 and 5 - Exception Handling and Packages Oop
Exception Handling
Exception Handling
• The Exception Handling in Java is one of the
powerful mechanism to handle the runtime errors so that
the normal flow of the application can be maintained.
• This means that code that might throw certain exceptions must
be enclosed by either of the following:
– A try statement that catches the exception.
try... catch
– A method that specifies that it can throw the exception.
throws or throw
– Programmer error,
19
Sample Program [1] cont.
• Compiles successfully but encounters a problem when it
runs
• ArrayIndexOutOfBoundException is a subclass of
What happens when exception
occurs?
• When an exception occurs within a method, the method creates an
object and hands it off to the runtime system.
The runtime system searches the call stack for a method that
contains a block of code that can handle the exception. This block of
code is called an exception handler.
The search begins with the method in which the error occurred and
proceeds through the call stack in the reverse order in which the methods
were called.
If the runtime system exhaustively searches all the methods on the call
SumNumbers [1]
public class SumNumbers {
public static void main(String[] arguments) {
float sum = 0;
for (int i=0; i < arguments.length; i++)
sum += Float.parseFloat(arguments[i]);
System.out.println("Those numbers add up to "+
sum);
}
}
23
SumNumbers [2]
public class SumNumbers {
public static void main(String[] arguments) {
float sum = 0;
for (int i=0; i < arguments.length; i++)
sum += Float.parseFloat(arguments[i]);
System.out.println("Those numbers add up to "+
sum);
}
}
24
Example [1]
import java.util.Scanner;
• Specifying the exception in the method signature: You can declare that a
method throws a checked exception by including the exception type in the
method signature using the "throws" keyword. This means that any code
calling the method must either handle the exception or declare it to be
thrown as well. public void someMethod() throws
SomeCheckedException {
// Code that may throw a checked
exception
Catching Exceptions in a try-catch
Block
try {
// Code that may throw an exception
} catch (ExceptionType1 ex1) {
// Exception handling for ExceptionType1
} catch (ExceptionType2 ex2) {
// Exception handling for ExceptionType2
} finally {
// Code that is always executed,
// regardless of whether an exception occurred or
not
NewSumNumbers [1]
public class NewSumNumbers {
public static void main(String[] arguments) {
float sum = 0;
for (int i=0; i<arguments.length; i++){
try {
sum += Float.parseFloat(arguments[i]);
}
catch (NumberFormatException e) {
System.out.println(arguments[i] + " isn’t a
number.");
}
}
System.out.println("Those numbers add up to "+ sum);
}
}
NewSumNumbers [Output]
Catching Several Different Exceptions
public class DivideNumbers {
public static void main(String[] arguments) {
if (arguments.length == 2) {
int result = 0;
try {
result =
Integer.parseInt(arguments[0])/Integer.parseInt(arguments[1]);
System.out.println("Result = " + result);
}
catch (NumberFormatException e) {
System.out.println("Both arguments must be numbers.");
}
catch (ArithmeticException e) {
System.out.println("You cannot divide by zero");
}
}
}
}
finally : Example
public class ExcepTest {
package animals;
package animals;
public class MammalInt implements Animal {
public void eat() {
interface Animal {
System.out.println("Mammal eats");
public void eat();
}
public void travel();
public void travel() {
}
System.out.println("Mammal
travels");
Compilation Process }
public int noOfLegs() {
return 0;
javac -d . Animal.java
}
javac -d . MammalInt.java public static void main(String args[]) {
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
The import Keyword
• Classes in the same package find each other without any
special syntax.
• Syntax
import packageName.*; (using .* wild card)
import packageName.className;
Example
package payroll;
public class Boss {
public void payEmployee(Employee e) {
e.mailCheck();
}
}
• if the Employee class is not in the payroll package? The Boss class must
then use one of the following techniques for referring to a class in a
different package.
import payroll.*;
import payroll.Employee;
Package Summary points
– A package is always defined as a separate folder having
the same name as the package name.
– Store all the classes in that package folder.