close

1.
class EX11_5 {
 public static void main(String [] args) {
  try {
   String a[]=new String[2];
   //a[0]="aa";
   //a[0]="81";
   //a[0]=a[0];
   double i = Double.parseDouble(a[0]);
   double j = Math.sqrt(i);
   System.out.println(i+" 的平方根為 "+j);
  }
  
  catch(NullPointerException ex) {//特異化的例外型別
   System.out.println("請加上一數值參數以求平方根");
   //System.exit(1);
  }
  
  catch(ArrayIndexOutOfBoundsException ex) {
  System.out.println("超過陣列範圍的參數以求平方根");
  //System.exit(1);
  }
  
  catch(NumberFormatException ex) {
  System.out.println("請加上整數參數");
  //System.exit(1);

  }
  
  
  //try{
  //}
  //catch(){}
  finally { System.out.println("本程式由 LSD 製作"); }
 }
}

2.
class QA11_3 {
 public static void main(String [] args) {
  try{
   return;
  }
  finally{
   System.out.println("QTL");
  }
 }
}
3.
class EX11_6 {
public static void main(String [] args) {
 try {
 //if(args.length   //double i = Double.parseDouble(args[0]);
  //double i=-1;
 double i=-9;
  if(i   double j = Math.sqrt(i);
  System.out.println(i+" 的平方根為 "+j);
 }
 catch(ArithmeticException ex) { System.out.println("發生例外0:"+ex); }
 catch(RuntimeException ex) { System.out.println("發生例外1:"+ex); }
 catch(Exception ex) { System.out.println("發生例外2:"+ex); }
}
}  
4.
import java.net.*;

class EX11_7 {
 public static void main(String [] args) throws MalformedURLException {
  //if(args.length   //else parseURL(args[0]);
  
  try{
   parseURL("
Telnet://tw.yahoo.com");
  }
  catch(MalformedURLException e){
   
   System.out.println("發生例外1:"+e);
  }
  catch(Exception e){
   System.out.println("產生錯誤2:"+e);
  }
  
  
 }
 static void parseURL(String str) throws MalformedURLException {
  URL url = new URL(str);
  System.out.println("使用協定:"+url.getProtocol());
  System.out.println("主機名稱:"+url.getHost());
 }
}

5.
import java.net.*;

public class EX11_8 {
public static void main(String [] args) {
  //if(args.length   //else {
try {
  //parseURL(args[0]);
  parseURL("
ftp://tw.yahoo.com");
}
catch(MyException ex) { //捕捉自訂例外物件
  ex.printStackTrace(); //印出呼叫堆疊的內容
  String s = ex.getMessage() + ex.getProtocol();
  System.out.println(s);
}
  catch(MalformedURLException ex) { System.out.println(ex.getMessage()); }
  catch(Exception e){System.out.println("產生錯誤2:"+e); }
// }
}
static void parseURL(String str) throws MalformedURLException {
  URL url = new URL(str);
  if(! url.getProtocol().equals("http")) throw new MyException("協定錯誤", url.getProtocol());
  System.out.println("主機名稱:"+url.getHost());
}
}  
6.
import java.io.*;
class test11_4{
 public static void main(String args[]){
  try{
   m();
   n();
  }
  catch (RuntimeException e){
   System.out.print("抓到例外RuntimeException:"+e.getMessage());
  }
  catch (EOFException e){
   System.out.print("抓到例外EOFException:"+e.getMessage());
  }
  catch (Exception e){
   System.out.print("抓到例外Exception:"+e.getMessage());
  }
 }
 static void m() { throw new RuntimeException("例外一");}
 //static void n() throws Exception { throw new EOFException("例外二");}
 static void n() throws Exception { throw new Exception("例外二");}
}

7.

import java.io.*;
public class test11_5{
 public static void main(String args[]){
  try{
   m();
  }
  catch(Exception e){
   
  }  
 }
 static void m() throws Exception{
  throw new EOFException();
 }
 
}
8.9.10.

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 凝輝 的頭像
    凝輝

    凝輝部落格

    凝輝 發表在 痞客邦 留言(0) 人氣()