顯示具有 JAVA 標籤的文章。 顯示所有文章
顯示具有 JAVA 標籤的文章。 顯示所有文章

2012年10月4日 星期四

[Java] 讀取輸入類別使用&輸出入例外IOException

InputStreamReader     ir = new InputStreamReder(System.in);
BufferedReader          br = new BufferedReader(ir);

String fileName;
try{
    
      fileName = br.readLine();
}catch(IOException e)   //輸出入處理錯誤例外
{
    System.out.println(e);
}

2012年10月3日 星期三

[Java]RuntimeException vs. Checked Exception vs. Error

例外類別架構圖

1. Runtime Exception
 程式設計師不小心或忘記產生.
 例如:
 
例外原因 例外名稱
陣列存取範圍例外ArrayIndexOutOfBoundsException
除0ArithmeticException
忘記將物件參考指定到一個物件實體NullPointerException 
指定陣列實體錯誤ArrayStoreExceptioin
類別型別轉換錯誤ClassCastException
方法參數傳遞錯誤IllegalArgumentException
宣告陣列大小為負值NativeArraySizeException
違反安全性限制SecurityException

2. Checked Exception
 外在因素所造成的,像是檔案存取錯誤,網路斷線等...
ps: 實際上並沒有Checked Exception 類別,凡不是RuntimeException 的例外,統稱Checked Exception.

常見的Checked Exception


例外原因 例外名稱
程式執行時,找不到所需的類別檔案ClassNotFoundException
找不到指定的檔案FileNotFoundException
執行緒中斷InterruptedException
輸出入處理錯誤IOException
資料庫處理錯誤SQLException

3. Error(例外處理能做的有限)
 屬於系統等級且非常嚴重的錯誤如 記憶體不足,執行緒死結
OutOfMemoryError
StackOverflowError
UnknowError
AWTError
ThreadDeadth


[101高考程式語言第四題]ExceptionQuestion




class FooException extends Exception{}
class ExceptionQuestion{
    public void aMethod()throws FooException{
       try{
          System.out.println("In aMethod");
          throw new FooException();   //產生例外
       }
       catch(FooException error)
       {
         System.out.println("in first catch");
         throw new FooException();     //例外產生不直接處理丟出
       }
       finally{
         System.out.println("Finally");    //finally 一定會執行
       }

    }
   
    public static void main(String[] args)
    {
       try{
          System.out.println("Start");   
          ExceptionQuestion x = new ExceptionQuestion();
          x.aMethod();     //發生例外了
          System.out.println("After method");
         
       }catch(FooException e1){
          System.out.println("In handler 1");
         
       }catch(Exception e2){
      
          System.out.println("In handler 2");
       }
       System.out.println("End");
   
   
   
   
    }

}

觀念
1. throws  關鍵字的使用(宣告一個方法可以丟出例外用 -> throws)
當例外產生時,不一定要立刻去處理它,可以再把例外丟給別人處理.

2. 當 try 區域的某一行程式碼產生例外之後,程式就會跳到catch區域中去執行例外處理的程式碼.

3. 有的程式碼不論有無發生例外都必需要執行的話,就放在finally區域
 




ans:

Start
In aMethod
in first catch
Finally
In handler 1
End

2012年9月25日 星期二

[Java]protected member can access by the smae package

//First.java

package abc;

public class First
{
   protected int value;
    public First()
    {
         value =10;
    }

}





//Second.java
package abc;

public class Second
{
  
    public Second(){}
    public void print()
    {
      First app = new First();
      System.out.println(app.value);
    }
   
}



//TestFS.java

import abc.First;
import abc.Second;

public class TestFS
{
   public TestFS()
   {
  
   }
  
   public static void main(String args[])
   {
      Second s = new Second();
      s.print();
   }



}

---------------------------------------------------------------
javac -d . First.java
javac -d . Second.java
javac TestFS.java
java  TestFS

2012年9月24日 星期一

[Java]產生的例外

Java  產生的例外都是一個物件(屬於Throwable類別或其子類別的副本)
Throwable 類別擁有兩個直接的子類別
1. Error  類別 : 屬於JVM 的嚴重錯誤,這種錯誤會導致程式終止執行,所以並無法使用例外處理處理這種錯誤.

2. Exception 類別:
 
  Excepton class 擁有一個子類別RuntimeException,這個類別的子類別是一些常見的例外.
 如
  ArithmeticException                          數學運算時產生的例外                        
  ArrayIndexOutOfBoundsException  陣列索引值小於0或超過陣列邊界
  ArrayStoreException                         陣列元數的型態不符
  IllegalArgumentException                 方法呼叫時參數型態不符
  NullPointerException                         物件值為null產生的例外


try{



}
catch(ExceptionType e)
{

}
finally{



}


//example 
public class DealMultiException
{
  
    public static void main(String args[])
    {
        try{
            int i;
            String args1 = args[0];  //產生超過陣列的例外
            for(i=2;i>-1;i--)
               System.out.println("計算結果:"+6/i); //Divide by zero exception
       

        }
        catch(ArithmeticException e)
        {
           //show exception info
           System.out.println("例外說明: "+e.getMessage());
           System.out.print("例外原因");
           e.printStackTrace();
        }
        catch(ArrayIndexOutOfBoundsException  e)
        {
           System.out.println("例外說明: "+e.getMessage());
           System.out.print("例外原因");
           e.printStackTrace();
        }
        finally
        {
            System.out.println("錯誤處理結束");
        }
        System.out.println("Programe end.");
   
   
    }



}


//丟出例外
public class MythrowException
{
   public static void main(String args[])
   {
           try
           {
             //取得參數字串
             int value = Integer.parseInt(args[0]);
           
             if(value==0)
               //丟出ArithmeticException 例外
               throw new ArithmeticException("值為0");

              
           }
           catch(ArithmeticException e)
           {
               //處理ArithmeticException例外
               System.out.println("例外說明:"+e.getMessage());
               System.out.print("例外原因 : ");
               e.printStackTrace();
           }
           finally
           {
              System.out.println("錯誤處理結束");
           }
   
   }



}

讓ASP.NET網站在開發除錯時期擁有WEB.CONFIG文件轉換功能

ref: https://blog.kkbruce.net/2016/09/enabling-xml-document-transforms-for-all-asp-net-web-by-develop-time.html#.XoP1KGMzbRY