Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
}
*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.