The Street Way

In java proramming handling exceptional circumstances.

In java programming handling exceptional circumstances.

Exception Handling

Always, A Program doesn't  run easly during its lifetime. Sometimes need to be handling unexpected errors in programs. many times user entered invalid inputs , networks connection drops, database fetching down and storage full, etc. such case as are referred as exception. which are programing stop execution normally.

As programmers we should handle these cases in order to present user interactions with users and let the program continues normally, instead of leaving  programing crashed or die, its called "EXCEPTION HANDLING".

Here we see the simple program of java : Exception Handling , using TRY & CATCH blocks.

SAVE this program same name of class -  Example : Exception.java (java is extension).
// declaring the class name
class Exception
{
    // main method ,and commad line arguments storage in string class.
    public static void main(String args[])
        {
        // variables declaration
        int a,b, ans;
        try
        {
            // passing value in interger value from command line argument and storage variable a
            a=Integer.parseInt(args[0]);
            // passing value in interger value from command line argument and storage variable b
            b=Integer.parseInt(args[1]);
            try
            {
                //arithmentic operations and calculations store value in the ans variable.
                ans = a/b;
                System.out.println(" Anser Is :"+ans); // display value of ans variable.
            }
            catch(ArithmeticException e) // if any exception in arithmeticException than passing this catch block.
            {
                System.out.println(" Divided  by Zero"); // display message of exception of arithmetic exception.
            }
        }
        catch(NumberFormatException e) // if any exception of invalid of inpur value than passing this catch block.
        {
            System.out.println(" Invalid value entered"); // display message of invalid value entered.
        }
    }
}


above programe output as below can from different type of value entered with arithmetic and invalid value.


here we have seen simple programe using nested try-catch blocks. 

exception handing use when errors come to run time when input value and any logical error generates that time programe terminates and close and not execution next statement, using exception handling we can pass errors and run all statements in programs.  

If you get this, then share with your friends. And if you have query, contact-me and give comment

 COMMENT!   SHARE.

0 comments:

Post a Comment