Exception Propagation
Exception Propagation
class Hello3{ }
void show(){
System.out.println("show() begins"); }
new A().m1();
System.out.println("show() ends");
}
}
class A{
void m1(){
System.out.println("A->m1() begins");
new B().m2();
System.out.println("A->m1() ends");
}
}
class B{
void m2(){
Lab564 Lab565
Lab566 Lab567
}
Lab568 }
}
public class Lab568 { class StudentNotFoundException extends
RuntimeException{
public static void main(String[] args) { StudentNotFoundException(String sid) {
System.out.println("main start"); super(sid);
String nm=""; }
try { }
StudentService serv=new StudentService();
//nm=serv.getNameBySid(null); Lab569
//
nm=serv.getNameBySid(""); public class Lab569 {
//
nm=serv.getNameBySid("jlc-088"); public static void main(String[] args) {
nm=serv.getNameBySid("jlc-099"); System.out.println("main started");
System.out.println("name :"+nm); try {
} catch (Exception e) { StudentService1 serv = new StudentService1();
e.printStackTrace(); serv.getNameBySid(null);
} } catch (Exception e) {
System.out.println("Main completed"); e.printStackTrace();
}
} }
}
}
class StudentService1{
class EmptySidException extends String getNameBySid(String sid){
RuntimeException{ if(sid==null||sid.isEmpty()||sid.equals("jlc-
099"))
} throw new StudentNotFoundException1(sid);
class StudentService{ else
String getNameBySid(String sid){ return "Srinivas";
if(sid==null) }
throw new NullPointerException(); }
else if(sid.isEmpty()) class StudentNotFoundException1 extends
throw new EmptySidException(); RuntimeException{
else if(sid.equals("jlc-099")) StudentNotFoundException1(String sid) {
return "Srinivas"; super(sid);
else }
throw new StudentNotFoundException(sid); }
Lab570 Lab571
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("main started"); System.out.println("main");
try { try {
StudentService2 serv = new StudentService2(); StudentService3 st = new StudentService3();
serv.getNameBySid(null); st.getNameById("ee");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
class StudentService3{
class StudentService2{ String getNameById(String sid) throws
String getNameBySid(String sid){ StudentNotFound{
if(sid==null||sid.isEmpty()||sid.equals("jlc- if(sid==null||sid.isEmpty()||!sid.equals("jlc-
099")) 099")){
throw new StudentNotFoundException1(sid); throw new StudentNotFound(sid);
else }else{
return "Srinivas"; return "rahul";
} }
} }
class StudentNotFoundException2 extends }
RuntimeException{ class StudentNotFound extends Exception{
StudentNotFoundException2(String sid) {
super(sid); public StudentNotFound(String sid){
} super(sid);
} }
}
Lab572 Lab573
package com;
public class Lab572 {
public class Lab573 {
public static void main(String[] args) { public static void main(String args[]){
Student stu[]=new Student[500]; System.out.println("Main started");
try { Thread th=Thread.currentThread();
for (int i = 0; i < stu.length; i++) { ThreadGroup tg=th.getThreadGroup();
stu[i]=new Student(); System.out.println(th.getId()+"\t"
System.out.println((i+1)+" object creation"); +th.getName()+"\t"+tg.getName());
} for(int i=0;i<args.length;i++){
} catch (Error e) { String st=args[i];
System.out.println("error Occured : "+ e); System.out.println(th.getId()+"\t"
} +th.getName()+"\t"+tg.getName()+"\t"+st);
System.out.println("\n after Handling"); }
Student st=new Student(); System.out.println("Main completed");
} }
} }
class Student{
long arr[]=new long[215833];
}
Lab574 Lab575
package com;
package com;
public class Lab574 {
public class Lab575 {
public static void main(String[] args) {
// TODO Auto-generated method stub public static void main(String[] args) {
MyThread t1=new MyThread(); // TODO Auto-generated method stub
MyThread t2=new MyThread(); MyThread1 th=new MyThread1();
t1.start(); Thread t1=new Thread(th);
t2.start(); Thread t2=new Thread(th);
Thread t=Thread.currentThread(); t1.start();
for(int i=1;i<=10;i++){ t2.start();
System.out.println(t.getName()+"- value is"+i); Thread t=Thread.currentThread();
try { for(int i=1;i<=10;i++){
Thread.sleep(500); System.out.println(t.getName()+"- value is"+i);
} catch (Exception e) { try {
e.printStackTrace(); Thread.sleep(500);
} } catch (Exception e) {
} e.printStackTrace();
} }
}
} }
}
class MyThread extends Thread{
public void run(){ class MyThread1 implements Runnable{
Thread th=Thread.currentThread(); public void run(){
for(int i=1;i<=10;i++){ Thread th=Thread.currentThread();
System.out.println(th.getName()+"- value for(int i=1;i<=10;i++){
is"+i); System.out.println(th.getName()+"- value
try { is"+i);
Thread.sleep(500); try {
} catch (Exception e) { Thread.sleep(500);
e.printStackTrace(); } catch (Exception e) {
} e.printStackTrace();
} }
} }
} }
}
Lab576 Lab577
package com;
package com;
public class Lab577 {
public class Lab576 {
public static void main(String[] args) {
public static void main(String[] args) { ThreadGroup tg=new ThreadGroup("Rahul");
// TODO Auto-generated method stub MyThread9 th1=new MyThread9();
ThreadGroup tg=new Thread th=new Thread(tg,th1,"roy");
ThreadGroup("WE"); th.start();
MyThread2 th1=new }
MyThread2(tg,"jlc-Thread"); }
MyThread2 th2=new
MyThread2(); class MyThread9 implements Runnable{
th1.start();
th2.start(); public void run(){
} Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
} int pri=th.getPriority();
class MyThread2 extends Thread{ for(int i=0;i<5;i++){
MyThread2(ThreadGroup tg, System.out.println(i+"\t"+th.getName()+"\t"
String name){ +tg.getName()+"\t"+pri);
super(tg,name); }
} }
MyThread2() { }
// TODO Auto-generated constructor stub
}
public void run(){
ThreadGroup tg=getThreadGroup();
int pri=getPriority();
for(int i=0;i<5;i++){
System.out.println(i+"\t"+getName()+
"\t"+tg.getName()+"\t"+pri);
}
}
}
Lab578 Lab579
package com;
package com;
public class Lab578 {
public class Lab579 {
public static void main(String[] args) {
MyThread10 th=new MyThread10(); public static void main(String[] args)
th.setPriority(2); throws Exception {
th.start(); // TODO Auto-generated method stub
MyThread3 th=new MyThread3();
} Thread t1=new Thread(th);
System.out.println(t1.getState());
} t1.start();
Thread.sleep(500);
class MyThread10 extends Thread{ System.out.println(t1.getState());
Thread.sleep(5000);
public void run(){ System.out.println(t1.getState());
ThreadGroup tg=getThreadGroup(); }
int pri=getPriority(); }
for(int i=0;i<5;i++){ class MyThread3 extends Thread{
System.out.println(i+"\t"+getName()+"\t" public void run(){
+tg.getName()+"\t"+pri); Thread th=Thread.currentThread();
} for(int i=1;i<=10;i++){
} System.out.println(th.getName()+
} "- value is:"+i+"\t"+th.getState());
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
Lab580 acc.withdraw(225);
if(acc.getBal()<0){
package com; System.out.println("Account is Overdrawn....");
class Account{ }
int bal=970; }
public void withdraw(int amt){ }
if(bal>=amt){ }
System.out.println(Thread.currentThread(). public class Lab580 {
getName()+" is going to withdraw...."+bal);
try { public static void main(String[] args) {
Thread.sleep(1200); // TODO Auto-generated method stub
} catch (Exception e) { Account acc=new Account();
// TODO: handle exception new AccountThread(acc);
} }
bal-=amt;
System.out.println(Thread.currentThread(). }
getName()+" is completed to
withdraw...."+bal);
} Lab581
else{
System.out.println("No Founds for "+ package com;
Thread.currentThread().getName());
} public class Lab581 {
}
public int getBal(){ public static void main(String[] args) {
return bal; // TODO Auto-generated method stub
} Hello h1=new Hello();
} Hello h2=new Hello();
MyThread4 th1=new MyThread4(h1);
class AccountThread implements Runnable{ MyThread4 th2=new MyThread4(h1);
Account acc=null; //MyThread4 th2=new MyThread4(h2);
AccountThread(Account acc) { th1.start();
this.acc=acc; th2.start();
Thread t1=new Thread(this,"A"); }
Thread t2=new Thread(this,"B"); }
t1.start(); class MyThread4 extends Thread{
t2.start(); Hello h=null;
}
public void run(){
for(int i=1;i<=5;i++){
MyThread4(Hello h) { th2.start();
// TODO Auto-generated constructor stub }
this.h=h;
} }
public void run(){
h.show(); class MyThread5 extends Thread{
} Hello1 h=null;
} MyThread5(Hello1 h) {
class Hello{ // TODO Auto-generated constructor stub
synchronized void show(){ this.h=h;
Thread th=Thread.currentThread(); }
for(int i=0;i<5;i++){ public void run(){
System.out.println(th.getName()+"- show: h.show();
"+i+"\t"+this); }
try { }
Thread.sleep(500); class Hello1{
} catch (Exception e) { void show(){
// TODO: handle exception Thread th=Thread.currentThread();
e.printStackTrace(); synchronized(this){
} for(int i=0;i<5;i++){
} System.out.println(th.getName()+"- show:
} "+i+"\t"+this);
} try {
Thread.sleep(500);
} catch (Exception e) {
Lab582 // TODO: handle exception
e.printStackTrace();
package com; }
}
public class Lab582 { }
}
public static void main(String[] args) { }
// TODO Auto-generated method stub
Hello1 h1=new Hello1();
Hello1 h2=new Hello1();
MyThread5 th1=new MyThread5(h1);
MyThread5 th2=new MyThread5(h1);
//MyThread4 th2=new MyThread4(h2);
th1.start();
Lab583 } catch (Exception e) {
// TODO: handle exception
package com; e.printStackTrace();
}
public class Lab583 { }
}
public static void main(String[] args) { }
// TODO Auto-generated method stub
Hello2 h1=new Hello2(); Lab584
Hello2 h2=new Hello2();
MyThread6 th1=new MyThread6(h1);
MyThread6 th2=new MyThread6(h1); package com;
th1.start();
th2.start(); public class Lab584 {
}
public static void main(String[] args) {
} // TODO Auto-generated method stub
Hello3 h1=new Hello3();
class MyThread6 extends Thread{ Hello3 h2=new Hello3();
Hello2 h=null; MyThread7 th1=new MyThread7(h1);
MyThread6(Hello2 h) { MyThread7 th2=new MyThread7(h1);
this.h=h; //MyThread4 th2=new MyThread6(h2);
} th1.start();
public void run(){ th2.start();
h.show(); }
}
} }
} package com;
} import java.util.ArrayList;
class Hello5{ }
synchronized void show(){
ArrayList al=new ArrayList(); class MyThread12 extends Thread{
Thread th=Thread.currentThread(); Hello6 h=null;
for(int i=0;i<5;i++){ public MyThread12(Hello6 h) {
try { this.h=h;
//wait(1000); }
//al.wait(1000); @Override
//Thread.sleep(1000); public void run() {
System.out.println(th.getName()+ h.show();
" - show:"+i+"\t"+this); }
} catch (Exception e) { }
// TODO: handle exception
e.printStackTrace(); class Hello6{
} void show(){
} ArrayList al=new ArrayList();
} Thread th=Thread.currentThread();
} synchronized (al){
for(int i=0;i<5;i++){
try { }
//wait(1000); this.x=x;
al.wait(1000); System.out.println(x+" is pushed..");
//Thread.sleep(1000); System.out.println(flag+" end push "+x);
System.out.println(th.getName()+ flag=true;
" - show:"+i+"\t"+this); //System.out.println(flag+" end push "+x);
} catch (Exception e) { notify();
// TODO: handle exception }
e.printStackTrace();
} public synchronized int pop(){
} if(!flag){
} System.out.println(flag+" begin pop "+x);
} try {
} wait();
} catch (Exception e) {
Lab588 e.printStackTrace();
}
package com; }
Lab593
package com;
System.out.println(getName() +
" Service Running "+ i+ "\t"+isDaemon());
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
try {
Lab594 Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
package com; }
System.out.println(th.getName()+
import java.util.concurrent. "\t task completed");
ExecutorService; }
import java.util.concurrent. }
Executors; }
package List;
import java.util.*; package List;
public class Lab615 { import java.util.*;
public static void main(String[] args) { public class Lab617 {
List list=new ArrayList(); public static void main(String[] args) {
list.add("sri"); List list=new ArrayList();
list.add("nivas"); list.add("sri");
list.add("dande"); list.add("nivas");
Iterator it=list.iterator(); list.add("dande");
int size=list.size(); list.add("java");
//list.remove("sri"); list.add("jdbc");
for(int i=0;i<=size;i++) list.add("jsp");
System.out.println(it.next()); ListIterator lit=list.listIterator();
System.out.println("main completed"); while(lit.hasNext()){
} Object obj=lit.next();
} System.out.println(obj);
}
Lab616 }
}
package List;
import java.util.*; Lab618
public class Lab616 {
public static void main(String[] args) {
List list=new ArrayList(); package List;
list.add("sri");
list.add("nivas"); import java.util.*;
list.add("dande");
Iterator it=list.iterator(); public class Lab618 {
while(it.hasNext()){ public static void main(String[] args) {
Object obj=it.next(); List list=new ArrayList();
System.out.println(obj); list.add("sri");
} list.add("nivas");
} list.add("dande");
} list.add("java");
list.add("jdbc");
list.add("jsp");
ListIterator lit=list.listIterator();
System.out.println(list); Lab620
while(lit.hasNext()){
Object obj=lit.next();
System.out.println(obj); package List;
if(obj.equals("nivas")) lit.remove(); import java.util.*;
else if(obj.equals("java")) lit.set("core java"); public class Lab620 {
else if(obj.equals("jsp")) lit.add("serviets"); public static void main(String[] args) {
} List list=new ArrayList();
System.out.println(list); list.add("sri");
} list.add("nivas");
} list.add("dande");
list.add("java");
Lab619 list.add("jdbc");
package List; list.add("jsp");
import java.util.*; ListIterator lit=list.listIterator(2);
public class Lab619 { do{
public static void main(String[] args) { int index=lit.nextIndex();
List list=new ArrayList(); Object obj=lit.next();
list.add("sri"); System.out.println(index+"\t"+obj);
list.add("nivas"); }
list.add("dande"); while(lit.hasNext());
list.add("java"); System.out.println();
list.add("jdbc"); lit=list.listIterator(5);
list.add("jsp"); do{
ListIterator lit=list.listIterator(); int index=lit.previousIndex();
System.out.println("forward order"); Object obj=lit.previous();
while(lit.hasNext()){ System.out.println(index+"\t"+obj);
int index=lit.nextIndex(); }while(lit.hasPrevious());
Object obj=lit.next(); }
System.out.println(index+"\t"+obj); }
}
System.out.println("reverse order");
while(lit.hasPrevious()){
int index=lit.nextIndex();
Object obj=lit.previous();
System.out.println(index+"\t"+obj);
}
}
}
Lab623
Lab621
package Set;
package Set; import java.util.*;
import java.util.*; public class Lab623 {
public class Lab621 { public static void main(String[] args) {
public static void main(String[] args) { TreeSet set=new TreeSet();
HashSet set=new HashSet(); set.add("sri");
set.add("sri"); set.add("nivas");
set.add("nivas"); set.add("dk");
set.add(99); set.add("manish");
set.add("dk"); System.out.println(set);
set.add("manish"); }
System.out.println(set); }
}
} Lab624
Lab634 Lab635
package Map;
package Set;
import java.util.LinkedHashMap;
public class Lab639 { import java.util.Map;
public static void main(String[] args) {
int i=2; public class Lab641 {
i++; public static void main(String[] args) {
Map map=new LinkedHashMap();
//int sum=0; map.put("eid", new Integer(99));
//sum=i+2; map.put("name", "srinivas");
//sum=sum+2; map.put("phone", new
System.out.println(i); Long(65799999l));
} map.put("valid", new
} Boolean(true));
System.out.println(map);
Lab640 System.out.println(map.
containsKey("eid"));
package Map; System.out.println(map.
import java.util.LinkedHashMap; containsKey("email"));
import java.util.Map; System.out.println(map.
public class Lab640 { containsValue("srinivas"));
public static void main(String[] args) { System.out.println(map.
Map map=new LinkedHashMap(); containsValue(new Integer(99)));
System.out.println(map); System.out.println(map.
System.out.println("Size : "+map.size()); containsValue("sd"));
System.out.println("is empty :
"+map.isEmpty()); }
map.put("eid", new Integer(99));
map.put("name", "srinivas"); }
map.put("phone", new Long(65799999l));
map.put("valid", new Boolean(true));
System.out.println(map);
System.out.println("Size : "+map.size());
System.out.println("is empty :
"+map.isEmpty());
}
}
Lab643 Lab645
package Collections;
package Collections;
import java.util.*;
import java.util.*;
public class Lab665 {
public static void main(String[] args) { public class Lab667 {
List<String> list=new public static void main(String[] args) {
ArrayList<>(); List<StringBuilder> list=new ArrayList<>();
list.add("nivas"); list.add(new StringBuilder("nivas"));
list.add("dande"); list.add(new StringBuilder("dande"));
list.add(new StringBuilder("anand"));
list.add("anand"); list.add(new StringBuilder("kumar"));
list.add("kumar"); list.add(new StringBuilder("dande"));
list.add("dande"); System.out.println(list);
System.out.println(list); Collections.sort(list,new StringBuilderComp());
System.out.println("****** System.out.println(list);
**reverse***********"); }
Collections.reverse(list); }
System.out.println(list);
System.out.println("****** class StringBuilderComp implements
**rotate(2)***********"); Comparator{
Collections.rotate(list, 2);
System.out.println(list); @Override
System.out.println("****** public int compare(Object o1, Object o2) {
**rotate(-3)***********"); String st=o1.toString();
Collections.rotate(list, -3); String st2=o2.toString();
System.out.println(list); return st.compareTo(st2);
System.out.println("****** }
**shuffle***********");
for (int i = 0; i < list.size(); i++) { }
Collections.shuffle(list);
System.out.println(list);
}
}
}
Lab669 Lab670
package ResourceBundle;
package Locale;
import java.io.IOException;
import java.util.Locale; import java.util.*;
public static void main(String[] args) { public static void main(String[] args)
Locale loc=Locale.getDefault(); throws IOException {
System.out.println(loc.getCountry()); String languageCode="en";
System.out.println(loc.getDisplayCountry()); System.out.println("H->Hindi , E->English");
System.out.println(loc.getLanguage()); System.out.println("enter your choise");
System.out.println(loc.getDisplayLanguage()); int asc=System.in.read();
Locale locs[]=Locale.getAvailableLocales(); char ch=(char) asc;
for (int i = 0; i < locs.length; i++) { ch=Character.toUpperCase(ch);
Locale L=locs[i]; if(ch=='H'){
System.out.println(L.getDisplayLanguage() languageCode="hi";
+"\t\t\t"+L.getDisplayCountry()); }else if (ch=='E') {
} languageCode="en";
}
} Locale loc=new Locale(languageCode);
ResourceBundle rsb=ResourceBundle.
} getBundle("messages", loc);
Enumeration ens=rsb.getKeys();
while (ens.hasMoreElements()) {
String key = (String)
ens.nextElement().toString();
String value=rsb.getString(key);
System.out.println(key+"\t\t"+value);
}
Lab680 Lab681
package Timer; package Scanner;
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("About to schedule task"); Scanner scr=new Scanner(System.in);
new Reminder(5); System.out.println("binery");
System.out.println("task schedule task"); int bin=scr.nextInt(2);
} System.out.println(bin);
} }
class Reminder{ }
Timer time;
public Reminder(int sec) {
time=new Timer(); Lab682
time.schedule(new RemainderTask(),
sec*1000); package Scanner;
}
import java.util.Scanner;
class RemainderTask extends TimerTask{
public class LAb682 {
public void run() {
public static void main(String[] args) {
System.out.println("times up"); String str="Hi Myself rahul ranjan";
time.cancel(); Scanner scr=new Scanner(str);
} while (scr.hasNext()) {
String string = (String) scr.next();
} System.out.println(string);
} }
package Formatter;
Lab710
import java.util.Formatter;
package com;
public class Lab683 {
public static void main(String[] args) { public class Lab710 {
Formatter ft=new Formatter(); public static void main(String[] args) {
String str=ft.format("Sum of %d Direction d1=null;
and %d is %d", 10,20,(10+20)).toString(); //d1=new Direction("EAST", 0);
System.out.println(str); d1=Direction.EAST;
ft.close(); System.out.println(d1);
} System.out.println(Direction.WEST);
} System.out.println(Direction.SOUTH);
System.out.println(Direction.NORTH);
Lab684 System.out.println(d1.angle);
}
package Formatter; }
package List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;