Java M4
Java M4
Packages
Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.
Defining a Package
Any classes declared within that file will belong to the specified package.
If you omit the package statement, the class names are put into the default package,
which has no name.
To do so, simply separate each package name from the one above it by use of a period.
Exception Handling
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.
Exception-Handling Fundamentals
Exception Types
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the
exception class hierarchy.
To handle this type of 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.
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.
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.
Primitive types, such as int or char, as well as non-Throwable classes, such as String and
Object, cannot be used as exceptions.
There are two ways you can obtain a Throwable object: using a parameter in a
catch clause or creating one with the new operator.
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.