Java J2ME JSP J2EE Servlet Android

Java by Example: Exception handling – throw custom Exception

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

Exception indicates illegal operation. So for your program you can define an operation as illegal. Say in your login program you allow maxim 3 times retry with wrong user/password. Then you can define your exception for attempt to login for more than thrice. And if this case occurs you can throw your exception.

Here is an example of throw custom exception to check if a number is a Fibonacci number or not. When a non Fibonacci number found exception 'Invalid fibonacci number' will be thrown..

Here is the example . .

public
class ExceptionExp4 {

    public
static
void main(String args[]){

        ExceptionExp4 excpExp = new ExceptionExp4();

        try{

            excpExp.tryCustomException();

        }catch(Exception ce){

            ce.printStackTrace();

        }

    }

    public
void tryCustomException() throws Exception{

        int fibs[]={1,2,3,5,8,10,13};

        for(int i=0;i<fibs.length;i++){

            if(isFibs(fibs[i]))

                System.out.println(fibs[i]+" - OK");

            else

                throw
new Exception("Invalid fibonacci number "+fibs[i]);

        }

    }

    private
boolean isFibs(int num){

        int f0 = 1;

        int f1 = 1;

        int fib = 1;

        while(num>=fib){

            if(num==fib)

                return
true;

            fib = f0 + f1;

            f0 = f1;

            f1 = fib;

        }

        return
false;

    }

}


 

Fibonacci number series is

1 2 3 5 8 13 21 34 . . . .

So if you try to execute exception should occur when 10 is found..

Lets see the output of this program...

1 - OK

2 - OK

3 - OK

5 - OK

8 - OK

java.lang.Exception: Invalid fibonacci number 10

    at example.java.lang.ExceptionExp4.tryCustomException(ExceptionExp4.java:18)

    at example.java.lang.ExceptionExp4.main(ExceptionExp4.java:7)


 

So in this way we can easily create our own exception with our own message as per our needs.

    
 


 


 


 


 


 


 


 

Java By Example: Exception Handling – Throwing exception from method

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

The second way of exception handling is throwing the exception. This is done by adding throws with method name.

Syntax of throwing exception from method is

access_modifier return_type method_name(arguments) throws exception_list{

}

Here is a practical example of doing this..

public
class ExceptionExp3 {

    public
static
void main(String args[]){

        ExceptionExp3 excpExp = new ExceptionExp3();

        try{

            excpExp.tryThrows();

        }catch(ArrayIndexOutOfBoundsException ae){

            System.out.println("Exception Caught");

        }

    }

    public
void tryThrows() throws ArrayIndexOutOfBoundsException{

        String strArray[] = {"This","is", "an", "example", "of", "Java","exception" };

        

            System.out.println(strArray[0]);

            System.out.println(strArray[1]);

            System.out.println(strArray[3]);

            System.out.println(strArray[4]);

            System.out.println(strArray[5]);

            System.out.println(strArray[6]);

            System.out.println(strArray[7]);

System.out.println("This statement wont execute");


 

    

    }

}

The ArrayIndexOutOfBoundsException is thrown from method tryThrows() instead of handling the exception. In this case when exception occurred in tryThrows() method instead of terminating the program this method throw the exception to caller.

This program will give the following output..

This

is

example

of

Java

exception

Exception Caught

Java by Example: Exception handling – Basic Exception Handling example ‘try catch’

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics


Exception Handling: The basic way to cope with exception is using try ...catch.

The Syntax for using try...catch is


 

try{

    ...

    ...

    Code have to monitor for exception

    ...

    ...

}catch(Exception e){

    ...

    Exception action code

    ...

}


 

Here is an easy example to demonstrate the above case..

public
class ExceptionExp2 {


 

    public
static
void main(String args[]){

        String strArray[] = {"This","is","an","example","of","Java","exception" };

        try{

            System.out.println(strArray[0]);

            System.out.println(strArray[1]);

            System.out.println(strArray[3]);

            System.out.println(strArray[4]);

            System.out.println(strArray[5]);

            System.out.println(strArray[6]);

            System.out.println(strArray[7]);

            


 

        }catch(ArrayIndexOutOfBoundsException exp){

            System.out.println("Exception Occurred");

        }

        System.out.println("This will execute");

    }

}


 

If you run the above code it will give the following output …

This

is

example

of

Java

exception

Exception Occurred

This will execute

So exception is handled here by printing a message "Exception Occurred" and the program continues to execute the codes after the try ... catch block instead of terminating.

Java by Example: Exception Handling – Java Exception Basics

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

Java provides strong way to cope with Errors and Exceptions. When an illegal operation occurred Java throws exception. And When exception occurs program execution ends if exception is not handled properly. There is a class below to understand Java Exception..


 

public
class ExceptionExp1 {


 

    public
static
void main(String args[]){

        String strArray[] = {"This","is", "an", "example", "of", "Java","exception" };

        System.out.println(strArray[0]);

        System.out.println(strArray[1]);

        System.out.println(strArray[3]);

        System.out.println(strArray[4]);

        System.out.println(strArray[5]);

        System.out.println(strArray[6]);

        System.out.println(strArray[7]);

        System.out.println("Program will terminate before executing this line");

    }

}


 

If you execute this program, the output will be-

This

is

example

of

Java

exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

    at example.java.lang.ExceptionExp1.main(ExceptionExp1.java:13)

Here exception occurs before ends the program and terminates.

Oracle : Hierarchical Data/ Tree in Oracle

Oracle provides strong way to query tree like data or hierarchical data.

The syntax for query hierarchical data is-


 

SELECT columns

FROM table_name

WHERE where_condition

CONNECT BY PRIOR condition_for_next_level

START WITH start_condition

ORDER SIBLINGS BY columns


 

Let's try it with a practical example.    


 

CREATE
TABLE location(

    id NUMBER,

    name
VARCHAR2(100),

    parent NUMBER

);


 

INSERT
INTO location(id,name,parent) VALUES(1,'Earth',0);

INSERT
INTO location(id,name,parent) VALUES(2,'Asia',1);

INSERT
INTO location(id,name,parent) VALUES(3,'Europe',1);

INSERT
INTO location(id,name,parent) VALUES(4,'America',1);

INSERT
INTO location(id,name,parent) VALUES(5,'Africa',1);

INSERT
INTO location(id,name,parent) VALUES(6,
'Oceania',1);


 

INSERT
INTO location(id,name,parent) VALUES(7,'Bangladesh',2);

INSERT
INTO location(id,name,parent) VALUES(8,'India',2);

INSERT
INTO location(id,name,parent) VALUES(9,'Pakistan',2);

INSERT
INTO location(id,name,parent) VALUES(10,'Singapore',2);

INSERT
INTO location(id,name,parent) VALUES(11,'Malaysia',2);


 

INSERT
INTO location(id,name,parent) VALUES(12,'England',3);

INSERT
INTO location(id,name,parent) VALUES(13,'France',3);

INSERT
INTO location(id,name,parent) VALUES(14,'Turkey',3);

INSERT
INTO location(id,name,parent) VALUES(15,'Scotland',3);


 

INSERT
INTO location(id,name,parent) VALUES(16,'Mexico',4);

INSERT
INTO location(id,name,parent) VALUES(17,'Canada',4);

INSERT
INTO location(id,name,parent) VALUES(18,'USA',4);


 

INSERT
INTO location(id,name,parent) VALUES(19,'Dhaka',7);

INSERT
INTO location(id,name,parent) VALUES(20,'Rajshahi',7);

INSERT
INTO location(id,name,parent) VALUES(21,'Chittagong',7);


 

INSERT
INTO location(id,name,parent) VALUES(22,'Las Vegas',18);

INSERT
INTO location(id,name,parent) VALUES(23,'New Orlince',18);

INSERT
INTO location(id,name,parent) VALUES(24,'New York',18);

Now

SELECT * FROM location;

Will give..

ID        NAME            PARENT

-----------------------------------------------

1        Earth            0

2        Asia            1

3        Europe         1

4        America         1

5        Africa            1

6        Oceania        1

7        Bangladesh        2

8        India            2

9        Pakistan        2

10        Singapore        2

11        Malaysia        2

12        England        3

13        France            3

14        Turkey            3

15        Scotland        3

16        Mexico            4

17        Canada            4

18        USA            4

19        Dhaka            7

20        Rajshahi        7

21        Chittagong        7

22        Las Vegas        18

23        New Orlince        18

24        New York        18


 

Now

SELECT
lpad(' ',8*(level-1))||NAME AS name
FROM LOCATION

CONNECT
BY
PRIOR id = parent

START
WITH parent = 0

Will give the following result

NAME

------------

Earth

Asia

Bangladesh

Dhaka

Rajshahi

Chittagong

India

Pakistan

Singapore

Malaysia

Europe

England

France

Turkey

Scotland

America

Mexico

Canada

USA

Las Vegas

New Orlince

New York

Africa

Oceania


 

You can also use ORDER SIBLINGS BY to sort siblings