class Test{
public void throwException(){
throw new Exception("aaa");
}
}
Main.java:18: 未报告的异常 java.lang.Exception;必须对其进行捕捉或声明以便抛出怎么样才能抛出去呢?
try
{
throw new Exception("aaa");
}catch(Exception e)
{
throw e;
}
也不行!
public void throwException(){
throw new Exception("aaa");
}
}
Main.java:18: 未报告的异常 java.lang.Exception;必须对其进行捕捉或声明以便抛出怎么样才能抛出去呢?
try
{
throw new Exception("aaa");
}catch(Exception e)
{
throw e;
}
也不行!
解决方案 »
- 用java实现一个方法来求出一个分类下面包括哪些品牌
- 做一个记事本遇到的问题
- runtime.exec 的奇怪问题!!!
- new Socket() 建立连接时间太长
- 如何用java实现自动批量打印
- 如何在jsrollpane的jtextpane中回车后,让光标不到下一行(急)
- Thread.start()的问题
- 各位高手,junit,ant,structs问题
- java.io.Serializable 的用处?????
- 这一贴,我只谈Tomcat数据连结池!(希望各位参观,并能有所收获)
- int num=b-'0'; 这句话什么意思?
- replaceAll("<br\\s*/?>|<p\\s*/?>|[\\s\\n]", "");请问这是什么意思?
public void throwException()throws Exception{
throw new Exception("aaa");
}
}
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Tester { /**
* @param args
*/
public static void main(String[] args) {
int a,b;
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("a:");
a=Integer.parseInt(reader.readLine());
System.out.print("b:");
b=Integer.parseInt(reader.readLine());
System.out.println("result="+ new Divider().perform(a, b));
} catch (NumberFormatException e) {
System.out.println("NumberFormatException found, msg:"+e.getMessage());
} catch (IOException e) {
System.out.println("IOException found, msg:"+e.getMessage());
} catch (MyException e) {
System.out.println("MyException found, msg:"+e.getMessage());
} catch(ArithmeticException e){
System.out.println("ArithmeticException found, msg:"+e.getMessage());
}
System.out.println("process end..");
}}
class Divider {
Float perform(int a, int b) throws ArithmeticException,MyException{
Float result=null;
if(a==12)
throw new MyException("12 is not a right number!");
result=(float)(a/b);
return result;
}
}public class MyException extends Exception {
public MyException(String message)
{
super(message);
}}
关于异常的常用几种写法。
代码实现的是输入两个数,后相除,输出结果。代码中有throws,有多重嵌套,有自定义异常等常用的异常格式。初学者可以好好看一看