java之異常

露水上的青蛙發表於2014-11-18

package com.text.exception;

class Test{
void add(int a,int b) throws Exception
{
int c;
c=a/b;
System.out.println(a+"/"+b+"="+c);
}
}
class DefaultException extends Exception
{
public DefaultException(String msg){
//呼叫Exception類的構造方法,存入異常資訊
super(msg);
}
}
public class TestException {

/* static void add(int a,int b) throws Exception
{
int c;
c=a/b;
System.out.println(a+"/"+b+"="+c);
}
*/
public TestException() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
//add(4,0);
try{
throw new DefaultException("自定義異常!");
}
catch(Exception e){
System.out.println(e);
}
/* Test t = new Test();

try{
t.add(5, 0);
}
catch(Exception e){
System.out.println("異常:"+e);
} */
// int arr[] = new int[5];
// try{
// arr[6] = 10;
// }
// catch(Exception e){
/* catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage().toString());
System.out.println("異常:"+e);
System.out.println(e.getLocalizedMessage());
}
*/

System.out.println("end of main() method!");
}

}