Pages

Java exception handling


Now, i want to share with you about exception handling in java. Okay, this is only for introduction to exception handling. Firstly, what is exception?

Exception - a person or thing that is excepted or that does not follow a rule.

So, if we talk about exception handling in java program, it mean we want to handle any mistake that we can handle during compile time or runtime.

For example :
-User put String into input field that only handle integer value.
-Out of memory.
-Array index that over than it's (length-1) or negative value. We also called it as ArrayIndexOutOfBoundsException

Why, i said "we want to handle any mistake that we can handle"?The answer is, there exist error that we can't handle.

For example :
-Blue screen of death(BSOD)
-Computer suddenly crash
-Black out
-Or someone switch off your computer

Ok, i hope you understand what i mean until this line. Why we need exception handling ? The answer is, we want to make sure our program is robust. It will try to handle any mistake that the user make.

Now, we will see two sample java programs. Both of them will ask user to input their age.After that, it will print it's value. This program only terminate when user put 0. Like we know, age is integer value. It can't be floating point number or String except your program need the user to put it in String. It is make not sense if when you ask someone for their age, and after that it answer like this "MY AGE IS 19.28". First program will has no exception handling. It will print some description about exception that occur and after that the program will end without follow program flow when the user put String value or others. But, in second program, it totally different. It will tell user that the input is wrong. After that, it will ask again until user put 0 to exit.Let we see both of them.Before i forget, if you put negative integer value, this program will work fine because it also integer. I will discuss later, about how to handle this in how to create our own exception handling.

FIRST PROGRAM :

import java.util.Scanner;

public class Test
{
public static void main(String[]args)
{
while(true)
{
Scanner scanUserInput=new Scanner(System.in);
System.out.println("Put 0 to exit");
System.out.println("What is your age ?");
int a=scanUserInput.nextInt();
if(a==0)
{
System.exit(0);
}
System.out.println("YOUR AGE IS : "+a);
}
}
}


SECOND PROGRAM :

import java.util.Scanner;
import java.util.InputMismatchException;

public class Test
{
public static void main(String[]args)
{
while(true)
{
try
{
Scanner scanUserInput=new Scanner(System.in);
System.out.println("Put 0 to exit");
System.out.println("What is your age ?");
int a=scanUserInput.nextInt();
if(a==0)
{
System.exit(0);
}
System.out.println("YOUR AGE IS : "+a);
}
catch(InputMismatchException exception)
{
System.out.println("Hey, don't play with me!");
System.out.println("PLEASE PUT ONLY INTEGER VALUE");
}
}
}
}


Related Posts Plugin for WordPress, Blogger...