My other sites |
New Summer Look :)
Java Exceptions and
Errors Exceptions Exception Handling Example Creating Exception Types Java's Packages Accessing Classes in Packages Packages and the CLASSPATH Access Modifiers Quiz Exercises |
|
![]() |
-There is a trade-off between correctness and clarity:
- Correctness, checking for errors - Clarity, a clear and readable basic flow of the code - Exceptions are a clean way to manage abnormal states - Exception : mild error which you can handle - Error : serious error -> termination - The idea is to signal exceptions to the current running process announcing that a problem has occurred - An exception is thrown and then caught |
![]() |
An exception arises in code at runtime Two subclasses of Throwable: - Exception - Error Un-handled exceptions -> Error (Catastrophic failure) |
//try - catch - finally Statement try { // block of code that may throw an exception. [ return; ] } catch (ExceptionType1 e) { // Exception handler for type1 } catch (ExceptionType2 e) { // Exception handler for type2 throw(e) // re-throw e Exception } finally { // clean-up code } |
![]() |
public class ExceptionExample { public static void main (String[] argv) { int [] numArray; // declaration numArray = new int[10]; // creation try { for (int x=0; x<numArray.length +1; x++) { numArray[x] = x * 2; System.out.print(numArray[x] + ":"); } } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("Array Exception caught!!"); } finally { System.out.println("Reached finally clause"); } } } |
![]() |
public class InvalidPasswordException extends Exception { InvalidPasswordException(String msg){ super(msg); } }public void loginUser(User usr) throws InvalidPasswordException { //Now the caller of loginUser is forced to provide a way to //handle the possible exception |
![]() |
Package is a: - Path to a directory containing a set of classes, - Name space, and - Naming mechanism |
![]() |
There are three ways to access classes in other packages:
1. Write the full name of the class: class ExitFrame extends java.awt.Frame { ... } 2. Import a specific class: import java.awt.Frame; class ExitFrame extends Frame { ... } 3. Import all exported classes from a package: import java.awt.*; class ExitFrame extends Frame { ... } |
![]() |
- CLASSPATH is a environmental variable that
holds directory paths to java classes - A class can belong to a package only if its source code declared membership with the package statement - To create and execute a class file belonging to a package : a. Declare package membership on the source code's 1st line b. Put the class file in an appropriate directory - i.e. if package cm.cmd.tool; then ../cm/cmd/tool/ c. Make sure that CLASSPATH knows the path to the classes in need - i.e. if package cm.cmd.tool; then ../cm/cmd/tool/ |
![]() |
For Methods and Fields :
|
![]() |
1. Class B can access a method of a class A which resides
in a different package. Class A is public and the method is protected.
What is the relationship between these two classes?
public class TestException { public static void main(String[] args) { int[] intArray= new int[10]; boolean result = fillArray(intArray); System.out.println("Result: " + result); } public static boolean fillArray(int[] intArray) { try { intArray[0] = 99; return true; } catch(Exception e) { System.out.println("Exc. accessing the array!"); } finally { return false; } } }
|
![]() |
1. Write three classes : Rectangle, Circle and Triangle,
all inheriting from an abstract class aShape. Add methods to calculate
the surface of those shapes |