/
C195C.java
84 lines (68 loc) · 2.13 KB
/
C195C.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.*;
import java.util.*;
/**
* */
public class C195C {
/**
* */
public class Block{
public Block parentBlock=null;
public List<Block> listChildren=Collections.synchronizedList(new ArrayList<Block>());
public Map<String,String> mapCatches=Collections.synchronizedMap(new HashMap<String,String>());
public Block(){}
public Block createBlock(){
Block childBlock=new Block();
childBlock.parentBlock=this;
this.listChildren.add(childBlock);
return childBlock;
}
public String handleThrow(String e){
if(this.mapCatches.containsKey(e))
return this.mapCatches.get(e);
return (this.parentBlock!=null)? this.parentBlock.handleThrow(e): "Unhandled Exception";
}
}
public Block newBlock(){
return new Block();
}
/**
* @param args
*/
public static void main(String[] args){
try {
Solution solution=new Solution();
BufferedReader reader =new BufferedReader( new InputStreamReader( System.in)); // STDIN
String s=reader.readLine();
Block rootBlock=solution.newBlock();
Block currentBlock=rootBlock;
Block throwBlock=null;
String throwException="";
while(s!=null){
s=s.trim();
// try
if(s.equals("try")){
currentBlock=currentBlock.createBlock();
}
// catch
if(s.startsWith("catch")){
String data[]=s.substring(s.indexOf("(")+1, s.indexOf(")")).split(",", 2);
String exceptionId=data[0].trim();
String exceptionMessage=data[1].trim().replaceAll("\"", "");
currentBlock.mapCatches.put(exceptionId, exceptionMessage);
currentBlock=currentBlock.parentBlock;
}
// throw
if(s.startsWith("throw")){
throwBlock=currentBlock;
throwException=s.substring(s.indexOf("(")+1,s.indexOf(")")).trim();
}
s=reader.readLine();
}
if(throwBlock!=null)
System.out.println(throwBlock.handleThrow(throwException));
else System.out.println("Unhandled Exception");
}catch(Exception e){
e.printStackTrace(System.err);
}
}
}