/**
* This code is from the book:
* Winder, R and Roberts, G (1998)
* Developing Java Software
* John Wiley & Sons.
* It is copyright (c) 1997 Russel Winder
* and Graham Roberts.
*/
class l1Except2{//Exception1
public int convert(String s){
int result = 0;
try{
//Use a method from the library class Integer
//to convert a string representing an integer
//to an integer value.
//parseInt will throw an exception if the
//String value is not convertable to an integer.
result = Integer.parseInt(s);
}
catch (NumberFormatException e){
System.out.println("String conversion failed: "+
e);
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
l1Except2 e1 = new l1Except2();
e1.convert("123");
e1.convert("abc");
}
}
/******** sample compilation & run *******
# javac l1Except2.java
# java l1Except2
String conversion failed: java.lang.NumberFormatException: abc
java.lang.NumberFormatException: abc
at java.lang.Integer.parseInt(Integer.java)
at java.lang.Integer.parseInt(Integer.java)
at l1Except2.convert(l1Except2.java:13)
at l1Except2.main(l1Except2.java:26)
#
******************************************/