Friday, April 12, 2019

exception handling

public class cl {
 public static void main(String args[]) {
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at cl.main(cl.java:4)

exception handling

{
 public static void main(String args[]) {
  String s="abc";
int i=Integer.parseInt(s);//NumberFormatException

  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.parseInt(Integer.java:615)
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  String s=null;
System.out.println(s.length());//NullPointerException
  System.out.println("End Of Main");
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NullPointerException
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
   public static void main(String args[]){
      int d = 0;
      int n = 20;
      int fraction = n/d;
     System.out.println("End Of Main");
   }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at cl.main(cl.java:5)

thread suspend()/resume()

public class cl extends Thread{
 public void run(){
  for(int i=1;i<=3;i++){
   try{
    Thread.sleep(500);
   }catch(Exception e){System.out.println(e);}
  System.out.println(i);
  }
 }
public static void main(String args[]){
 cl t1=new cl();
 cl t2=new cl();
 cl t3=new cl();
 t1.start();
 t2.start();
 t3.start();
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
1
1
1
2
2
2
3
3
3




public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 
 t1.start(); 
 t2.start(); 
 t2.suspend();
 t3.start(); 
 t2.resume();
 } 
}

daemon thread

public class cl extends Thread{
 public void run(){
  if(Thread.currentThread().isDaemon()){
   System.out.println("daemon thread");
  }
  else{
  System.out.println("user thread ");
 }
 }
 public static void main(String[] args){
  cl t1=new cl();
  cl t2=new cl();
  cl t3=new cl();

  t1.setDaemon(true);
 
  t1.start();
  t2.start();
  t3.start();
 }
}

$javac cl.java
$java -Xmx128M -Xms16M cl
daemon thread
user thread 
user thread 

set priority of thread


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
  t1.setPriority(Thread.MIN_PRIORITY); 
  t2.setPriority(Thread.MAX_PRIORITY);
 
 t1.start(); 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:1
1
Thread-1 running thread priority is:10
1
Thread-2 running thread priority is:5
1
Thread-0 running thread priority is:1
2
Thread-1 running thread priority is:10
2
Thread-2 running thread priority is:5
2
Thread-0 running thread priority is:1
3
Thread-1 running thread priority is:10
3
Thread-2 running thread priority is:5
3

thread priority


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:5
1
Thread-0 running thread priority is:5
2
Thread-0 running thread priority is:5
3
Name of t1:dj thread
Thread-1 running thread priority is:5
1
Thread-2 running thread priority is:5
1
Thread-1 running thread priority is:5
2
Thread-2 running thread priority is:5
2
Thread-1 running thread priority is:5
3
Thread-2 running thread priority is:5
3

current thread name

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" "); 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 1
Thread-0 2
Thread-0 3
Name of t1:dj thread
Thread-1 1
Thread-2 1
Thread-1 2
Thread-2 2
Thread-1 3
Thread-2 3