Sei sulla pagina 1di 7

give the input from command prompt->

Actually java takes every input as a String form so we use some type cast method
for integer->int a =Integer.parseInt(args[0]);
int b =Integer.parseInt(args[1]);
for long ->long a1 = Long.parseLong(args[0]);
for float->float a2 = Float.parseFloat(args[0]);
for double->double a2 = Double.parseDouble(args[0]);
for String->String name = args[0];
ex->
public class Name {
public static void main(String[] args) {
String name = args[0];
int a =Integer.parseInt(args[1]);
int b =Integer.parseInt(args[1]);
long l1 = Long.parseLong(args[2]);
float f1 = Float.parseFloat(args[3]);
double d1 = Double.parseDouble(args[4]);
System.out.println("name is :"+name);
System.out.println("integer is :"+a+"\t"+b);
System.out.println("long is :"+l1);
System.out.println("float is :"+f1);
System.out.println("double is :"+d1);
}
}
Exception->An Exception is an abnormal condition that occurs during the executio
n of the program and interrupts the normal flow of program execution
what will happen if you are compile and execute the following code
ex->
public class Divide {
public static void main(String[] args) {
int a =Integer.parseInt(args[0]);
int b= Integer.parseInt(args[1]);
float c =a/b;
System.out.println("divison is "+c);
}
}
Ans->(1)the code will be compiled successfully
(2)but at run time follwoing case can be happen
case 1-> java Divide

(when value is not provided from command line)

o/p->Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:


case 2->java Divide a b
o/p->Exception in thread "main" java.lang.NumberFormatException
case 3->java Divide 5 0
o/p->Exception in thread "main" java.lang.ArithmeticException:
common scanario of Exception Handling->
-------------------------------------1-> int a =50/0; //ArithmeticException
2->String s =null;
System.out.println(s.length()); //NullPointerException
3->String s = "abc";
int i =Integer.parseInt(s); //NumberFormatException
4->int a[] = new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException
*Type of error->(1)compile time
(2)run time
*note->(1)Run time error result in an Exception
(2)whenever Exception occur java create an object of "Exception" or its d
erive class
(3)Exception leads to abnormal termination of program
(4)handling means making the user aware with the problem.
(5)Throwable is the base class of all Exception
Object
---------|
Throwable
--------------------------------------|
|
Exception
Error-(1)OutOfMemoryError
--------------------------------------(2)StackOverFlowError
|
|
(3)VirtualMachineError
Checked Exception
RunTimeException
:
|
|
etc
(1)ClassNotFoundException
Unchecked Exception
(2)SQLException
|
(3)IOException
(1)ArithmeticException
:
(2)NullPointerException
:
(3)ArrayIndexOutOfBoundsException
etc
(4)NumberFormatException
(5)ClassCastException
:
:
etc
*CheckedException->(1)CheckedException are checked at compile time
(2)CheckedException is direct sub-Class of Exception
(3)these are also called compile time exception

(4)CheckedException must be reported in any of the form


(A)Handling the exception with try-catch
(B)Propagating the exception using throws
*UncheckedException->(1)UncheckedException are not checked at compile time rathe
r
they are checked at run time.
(2)UnChecked Exception is direct sub-Class of RunTimeExcept
ion
(3)these are also called run time exception
Error->it can not be handle(unrecoverable)
Handling Exception in program->
------------------------------(1)when JVM handles the exception using default exception handler then program e
xecution
will be terminated abnormally after displaying message
(2)when you want the normal termination of the program after executin all requir
ed statements
then you have to handle the exception
(3)you can use the following two keywords to handle the exception
(A)try
(B)catch
try block->(1)All statement that may result an Exception are written within try
block
-----------(2)A method can have nested or multiple try block
(3)try block must be followed by either catch or finally block
(A)syntax of try with catch block->
try
{
------------------------------}
catch(Exception_class_name )
{
--------}
(B)syntax of try with finally block->
try
{
------------------------------}
finally
{
--------}
catch block->(1)catch block is used to handle the Exception.it must be used
------------- after the try block.
(2)multiple catch blocks works like case in switch and only one
will be executed depending on Exception generated.

(3)catch receives an argument of Exception or drive class.


finally block->(1)finally block is a block that is always executed even
--------------

Exception occur or not.


(2)there can be only one finally.
(3)finally does not receive any argument.
(4)finally must be followd by try or catch block.

ex.1->public class Divide {


public static void main(String[] args) {
float c=0f;
try
{
int a =Integer.parseInt(args[0]);
int b= Integer.parseInt(args[1]);
c =a/b;
}
catch(ArithmeticException ae)
{
System.out.println("divison by zero");
}
catch(ArrayIndexOutOfBoundsException ai)
{
System.out.println("no input");
}
catch(NumberFormatException ne)
{
System.out.println("wrong input");
}
catch(Exception e)
{
e.printStackTrace();
//System.out.println(e);
}
System.out.println("divison is "+c);
}
}
Example of finally->
-------------------public class Divide {
public static void main(String[] args) {
float c=0f;
try
{
int a =Integer.parseInt(args[0]);
int b= Integer.parseInt(args[1]);
c =a/b;
}
finally
{
System.out.println("mera bharat mahan");
}
System.out.println("divison is "+c);
}

}
*throw->(1)throw is a keyword
--------(2)you can throw any checked or unchecked exception
(3)you can throw any Bulit-int or custom exception
custom exception->(1)if you are creating your own exception known as custom
or user define exception.
(2)if you want to write custom exception it must be inherit
the Exception class
example of custom exception->
----------------------------ex.2->
public class MyException extends Exception {
static int accno[]={100,101,102,103,104,105};
static String name[]={"raja","rama","subh","anay","lakshmi"};
static long bal[]={1000,12000,1335,999,3500};
public static void main(String[] args) {
try
{
System.out.println("ACCNO"+"\t"+"CUSTOMER"+"\t"+"BALANCE
");
for(int i=0;i<5;i++)
{
System.out.println(accno[i]+"\t"+name[i]+"\t"+b
al[i]);
if(bal[i]<1000)
{
MyException me = new MyException();
throw me;
}
}
}
catch(MyException me)
{
System.out.println("balance amount is less");
me.printStackTrace();
}
}
}
*throws->(1)throws is keyword
---------(2)throws is used to propagate the exceptions to the caller method by
specifying at method level
(3)you can define any checked or unchecked exception
(4)you can define any Built-in or custom exception at method level
(5)when the exception is unchecked exception then throws keyword is opt
onal
but for the checked exception throws keyword is mandatory
ex.1->import java.util.*;
class InvalidAgeException extends Exception
{

InvalidAgeException(String s)
{
super(s);
}
}
class Excep
{
static void validate(int age)throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String[] args) {
try
{
Scanner s1 = new Scanner(System.in);
System.out.println("enter age");
int age = s1.nextInt();
validate(age);
}
catch(Exception e)
{
System.out.println("exception occured :"+e);
}
}
}
ex.2->public class MyExplicitThrow {
public static void main(String a[]){
try{
MyExplicitThrow met = new MyExplicitThrow();
System.out.println("length of JUNK is "+met.getStringSize("JUNK"));
System.out.println("length of WRONG is "+met.getStringSize("WRONG"))
;
System.out.println("length of null string is "+met.getStringSize(nul
l));
} catch (Exception ex){
System.out.println("Exception message: "+ex.getMessage());
}
}
public int getStringSize(String str) throws Exception{
if(str == null){
throw new Exception("String input is null");
}
return str.length();
}
}
Questions->
----------*(1)diff b/w throw and throws
*(2)it is necessary that each try block must be followed by a catch block
*(3)can a exception be rethrown
*(4)which exception should be declare
*(5)if i write "return" at the end of the try will the finally block still execu
te.

*(6)if i write "System.exit(0)" at the end of the try will the finally block sti
ll execute.
*(7)what will happen if static modifier is remove from the signature of the main
method.
*(8)what will happen if i do not provide the String array as argument from the s
ignature of the main method.

Potrebbero piacerti anche