异常抛出问题!!

charmlin 2008-06-01 09:37:05
import java.io.*;
class MyException extends Exception{
private String reason;
public MyException(String r){
reason=r;
}
public String getReason(){
return(reason);
}
}
class Excp{
private String s;
public Excp(String ss)throws MyException{
s=ss;
if(s=="XYZ"){
throw new MyException("This is a XYZ");
}
}
}
class Test_Excp{
public static void main(String args[])throws Exception{

byte buffer[]=new byte[255];
System.out.println("Plz input s1:");
System.in.read(buffer,0,255);
String s1=new String(buffer);
try{
Excp e1=new Excp(s1);
}
catch(MyException e){
System.out.println("ddfd");
}
}
}
...全文
80 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
charmlin 2008-06-01
  • 打赏
  • 举报
回复
恩,明白了
blazingfire 2008-06-01
  • 打赏
  • 举报
回复
in.readLine();时会抛出IOException异常的
blazingfire 2008-06-01
  • 打赏
  • 举报
回复
用标准输入流录入的地方要改要好一些。或者改成如下:
.....
System.in.read(buffer,0,255);
String s1=new String(buffer).trim(); //那个Buffer含有无效的字符
.................
charmlin 2008-06-01
  • 打赏
  • 举报
回复
还有怎么不能把main后面的抛出删掉呢,函数里不是已经捕捉了吗?
charmlin 2008-06-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 blazingfire 的回复:]
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s1 = in.readLine();
[/Quote]
为什么要用这个啊
blazingfire 2008-06-01
  • 打赏
  • 举报
回复

import java.io.*;

class MyException extends Exception {
private String reason;

public MyException(String r) {
reason = r;
}

public String getReason() {
return (reason);
}
}

class Excp {
private String s;

public Excp(String ss) throws MyException {
s = ss;
if (s.equals("XYZ")) {
throw new MyException("This is a XYZ");
}
}
}

public class Test_Excp {
public static void main(String args[]) throws Exception {
System.out.println("Plz input s1:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s1 = in.readLine();
try {
Excp e1 = new Excp(s1);
} catch (MyException e) {
System.out.println("ddfd");
}
}
}
charmlin 2008-06-01
  • 打赏
  • 举报
回复
大仙们!up
charmlin 2008-06-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhj92lxs 的回复:]
s=="XYZ"
换成"XYZ".equals(s)
[/Quote]
换了还是没用
charmlin 2008-06-01
  • 打赏
  • 举报
回复
还有一个问题,怎么不能把main后面的抛出删掉呢,函数里不是已经捕捉了吗?
zhj92lxs 2008-06-01
  • 打赏
  • 举报
回复
s=="XYZ"
换成"XYZ".equals(s)
charmlin 2008-06-01
  • 打赏
  • 举报
回复
运行后没有捕捉到异常MyException,怎么回事
jason_kou 2008-06-01
  • 打赏
  • 举报
回复
不喜欢猜问题。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧