public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub if(args.length==0){ new NoOprandException(); }else if(args.length==1){ new OnlyOneException(); }else{ System.out.println(args[0]+args[1]); } }} public class NoOprandException extends Exception {} public class OnlyOneException extends Exception {}
不好意思,,修改下public class Test { /** * @param args * @throws NoOprandException */ public static void main(String[] args) { // TODO Auto-generated method stub if(args.length==0){ try { throw new NoOprandException(); } catch (NoOprandException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if(args.length==1){ try { throw new OnlyOneException(); } catch (OnlyOneException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println(args[0]+args[1]); } }} public class OnlyOneException extends Exception { @Override public void printStackTrace() { // TODO Auto-generated method stub System.out.println("NoOprandException"); super.printStackTrace(); }} public class NoOprandException extends Exception { @Override public void printStackTrace() { // TODO Auto-generated method stub System.out.println("OnlyOneException"); super.printStackTrace(); }}
自己定义好异常,
将参数从args里面读出来,public static void main(String[] args){
}
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length==0){
new NoOprandException();
}else if(args.length==1){
new OnlyOneException();
}else{
System.out.println(args[0]+args[1]);
}
}}
public class NoOprandException extends Exception {}
public class OnlyOneException extends Exception {}
* @param args
* @throws NoOprandException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length==0){
try {
throw new NoOprandException();
} catch (NoOprandException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(args.length==1){
try {
throw new OnlyOneException();
} catch (OnlyOneException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(args[0]+args[1]);
}
}}
public class OnlyOneException extends Exception { @Override
public void printStackTrace() {
// TODO Auto-generated method stub
System.out.println("NoOprandException");
super.printStackTrace();
}}
public class NoOprandException extends Exception { @Override
public void printStackTrace() {
// TODO Auto-generated method stub
System.out.println("OnlyOneException");
super.printStackTrace();
}}