Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Throwing Exceptions

We have seen till now how exceptions thrown by methods we invoke such as nextInt() can be caught. We shall now see how we ourselves can throw exceptions. An exception is thrown by specifying the throw keyword followed by an object reference.

throw <object>;

Only objects that have been created from classes which have implemented the Throwable interface can be thrown. An object of an Exception can be created just like any other object of a class by using one of the four forms of the constructor, which every Exception type inherits from the Throwable class. We shall see two of them for now. One of these is the default constructor and the other is the parameterised constructor which accepts a String which acts as a description for the exception object we are creating. The following statement shows the use of these two forms of the constructor to create an Exception object.

Exception e = new Exception();
Exception e = new Exception ( "This is an Exception");

The objects created above may be thrown by using the throw keyword.

throw e;

We can also combine the statement that creates the Exception and the statement which throws the Exception into a single statement.

throw new Exception();

The following program shows the use of the throw keyword.

public class ThrowException {

    public static void main(String args[]){
      throw new Exception();
     } catch(Exception e) {
      System.out.println("Exception caught");

The output of this program would be

Exception caught

Privacy Policy