遍匈 / Java縮殻 /

Java 呟械侃尖

<貧匯准 和匯准>
呟械祥頁峺殻會峇佩扮窟伏議諒籾。
呟械窟伏議圻咀嗤載謹?宥械淫根參和叱寄窃?
  • 喘薩補秘阻掲隈方象。
  • 勣嬉蝕議猟周音贋壓。
  • 利大宥佚扮銭俊嶄僅?賜宀JVM坪贋吝竃。
宸乂呟械嗤議頁咀葎喘薩危列哈軟?嗤議頁殻會危列哈軟議?珊嗤凪万匯乂頁咀葎麗尖危列哈軟議。-
勣尖盾Java呟械侃尖頁泌採垢恬議?低俶勣嫺燐參和眉嶽窃侏議呟械?
  • 殊臥來呟械?恷醤旗燕議殊臥來呟械頁喘薩危列賜諒籾哈軟議呟械?宸頁殻會埀涙隈圓需議。箭泌勣嬉蝕匯倖音贋壓猟周扮?匯倖呟械祥窟伏阻?宸乂呟械壓園咎扮音嬬瓜酒汽仇策待。
  • 塰佩扮呟械? 塰佩扮呟械頁辛嬬瓜殻會埀閲窒議呟械。嚥殊臥來呟械屢郡?塰佩扮呟械辛參壓園咎扮瓜策待。
  • 危列? 危列音頁呟械?遇頁用宣殻會埀陣崙議諒籾。危列壓旗鷹嶄宥械瓜策待。箭泌?輝媚吝竃扮?匯倖危列祥窟伏阻?万断壓園咎匆殊臥音欺議。

Exception窃議蚊肝?

侭嗤議呟械窃頁貫java.lang.Exception窃写覚議徨窃。
Exception窃頁Throwable窃議徨窃。茅阻Exception窃翌?Throwable珊嗤匯倖徨窃Error 。
Java殻會宥械音俺資危列。危列匯違窟伏壓冢嶷絞嬾扮?万断壓Java殻會侃尖議袈葛岻翌。
Error喘栖峺幣塰佩扮桟廠窟伏議危列。
箭泌?JVM坪贋吝竃。匯違仇?殻會音氏貫危列嶄志鹸。
呟械窃嗤曾倖麼勣議徨窃?IOException窃才RuntimeException窃。 壓Java 坪崔窃嶄?嗤寄何蛍械喘殊臥來才掲殊臥來呟械。

呟械圭隈?

和中議双燕頁Throwable 窃議麼勣圭隈?
會催 圭隈式傍苧
1 public String getMessage()
卦指購噐窟伏議呟械議峋聾佚連。宸倖嶧連壓Throwable 窃議更夛痕方嶄兜兵晒阻。
2 public Throwable getCause()
卦指匯倖Throwable 斤嵆旗燕呟械圻咀。
3 public String toString()
聞喘getMessage()議潤惚卦指窃議堪雫兆忖。
4 public void printStackTrace()
嬉咫toString()潤惚才媚蚊肝欺System.err?軸危列補竃送。
5 public StackTraceElement [] getStackTrace()
卦指匯倖淫根均媚蚊肝議方怏。和炎葎0議圷殆旗燕媚競?恷朔匯倖圷殆旗燕圭隈距喘均媚議媚久。
6 public Throwable fillInStackTrace()
喘輝念議距喘媚蚊肝野割Throwable 斤嵆媚蚊肝?耶紗欺媚蚊肝販採枠念佚連嶄。

俺資呟械?

聞喘trycatch購囚忖辛參俺資呟械。try/catch旗鷹翠慧壓呟械辛嬬窟伏議仇圭。
try/catch旗鷹翠嶄議旗鷹各葎隠擦旗鷹?聞喘 try/catch議囂隈泌和?
try
{
 //Protected code
}catch(ExceptionName e1)
{
 //Catch block
}
Catch囂鞘淫根勣俺資呟械窃侏議蕗苧。輝隠擦旗鷹翠嶄窟伏匯倖呟械扮?try朔中議catch翠祥氏瓜殊臥。
泌惚窟伏議呟械淫根壓catch翠嶄?呟械氏瓜勧弓欺乎catch翠?宸才勧弓匯倖歌方欺圭隈頁匯劔。

幣箭?

和中議箭徨嶄蕗苧嗤曾倖圷殆議匯倖方怏?輝旗鷹編夕恵諒方怏議及眉倖圷殆議扮昨祥氏砺竃匯倖呟械。
// File Name : ExcepTest.java
import java.io.*;
public class ExcepTest{
 
 public static void main(String args[]){
 try{
 int a[] = new int[2];
 System.out.println("Access element three :" + a[3]);
 }catch(ArrayIndexOutOfBoundsException e){
 System.out.println("Exception thrown :" + e);
 }
 System.out.println("Out of the block");
 }
}
塰佩潤惚泌和?
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block

謹嶷俺資翠?

匯倖try旗鷹翠朔中効昧謹倖catch旗鷹翠議秤趨祥出謹嶷俺資。
謹嶷俺資翠議囂隈泌和侭幣?
try
{
 //Protected code
}catch(ExceptionType1 e1)
{
 //Catch block
}catch(ExceptionType2 e2)
{
 //Catch block
}catch(ExceptionType3 e3)
{
 //Catch block
}
貧中議旗鷹粁淫根阻3倖catch翠。
辛參壓ry囂鞘朔中耶紗販吭方楚議catch翠。
泌惚隠擦旗鷹嶄窟伏呟械?呟械瓜砺公及匯倖catch翠。
泌惚砺竃呟械議方象窃侏嚥ExceptionType1謄塘?万壓宸戦祥氏瓜俺資。
泌惚音謄塘?万氏瓜勧弓公及屈倖catch翠。
泌緩?岷欺呟械瓜俺資賜宀宥狛侭嗤議catch翠。

幣箭?

乎糞箭婢幣阻奕担聞喘謹嶷try/catch。
try
{
 file = new FileInputStream(fileName);
 x = (byte) file.read();
}catch(IOException i)
{
 i.printStackTrace();
 return -1;
}catch(FileNotFoundException f) //Not valid!
{
 f.printStackTrace();
 return -1;
}

throws/throw購囚忖?

泌惚匯倖圭隈短嗤俺資匯倖殊臥來呟械?椎担乎圭隈駅倬聞喘throws 購囚忖栖蕗苧。throws購囚忖慧壓圭隈禰兆議硫何。
匆辛參聞喘throw購囚忖砺竃匯倖呟械?涙胎万頁仟糞箭晒議珊頁胡俺資欺議。
和中圭隈議蕗苧砺竃匯倖RemoteException呟械?
import java.io.*;
public class className
{
 public void deposit(double amount) throws RemoteException
 {
 // Method implementation
 throw new RemoteException();
 }
 //Remainder of class definition
}
匯倖圭隈辛參蕗苧砺竃謹倖呟械?謹倖呟械岻寂喘矯催侯蝕。
箭泌?和中議圭隈蕗苧砺竃RemoteException才InsufficientFundsException?
import java.io.*;
public class className
{
 public void withdraw(double amount) throws RemoteException,
 InsufficientFundsException
 {
 // Method implementation
 }
 //Remainder of class definition
}

finally購囚忖

finally購囚忖喘栖幹秀壓try旗鷹翠朔中峇佩議旗鷹翠。
涙胎頁倦窟伏呟械?finally旗鷹翠嶄議旗鷹悳氏瓜峇佩。
壓finally旗鷹翠嶄?辛參塰佩賠尖窃侏吉辺硫鋲朔來嵎議囂鞘。
finally旗鷹翠竃孖壓catch旗鷹翠恷朔?囂隈泌和?
try
{
 //Protected code
}catch(ExceptionType1 e1)
{
 //Catch block
}catch(ExceptionType2 e2)
{
 //Catch block
}catch(ExceptionType3 e3)
{
 //Catch block
}finally
{
 //The finally block always executes.
}

幣箭?

public class ExcepTest{
 
 public static void main(String args[]){
 int a[] = new int[2];
 try{
 System.out.println("Access element three :" + a[3]);
 }catch(ArrayIndexOutOfBoundsException e){
 System.out.println("Exception thrown :" + e);
 }
 finally{
 a[0] = 6;
 System.out.println("First element value: " +a[0]);
 System.out.println("The finally statement is executed");
 }
 }
}
塰佩潤惚泌和?
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
廣吭和中並??
  • catch音嬬鏡羨噐try贋壓。
  • 壓try/catch朔中耶紗finally翠旺掲膿崙來勣箔議。
  • try旗鷹朔音嬬屡短catch翠匆短finally翠。
  • try, catch, finally翠岻寂音嬬耶紗販採旗鷹。

蕗苧徭協吶呟械?

壓Java嶄低辛參徭協吶呟械。園亟徭失議呟械窃扮俶勣芝廖和中議叱泣。
  • 侭嗤呟械脅駅倬頁Throwable議徨窃。
  • 泌惚錬李亟匯倖殊臥來呟械窃?夸俶勣写覚Exception窃。
  • 泌惚低?亟匯倖塰佩扮呟械窃?椎担俶勣写覚RuntimeException 窃。
辛參崧和中宸劔協吶徭失議呟械窃?
class MyException extends Exception{
}
峪写覚Exception 窃栖幹秀議呟械窃頁殊臥來呟械窃。
和中議InsufficientFundsException窃頁喘薩協吶議呟械窃?万写覚徭Exception。
匯倖呟械窃才凪万販採窃匯劔?淫根嗤延楚才圭隈。

幣箭?

// File Name InsufficientFundsException.java
import java.io.*;
 
public class InsufficientFundsException extends Exception
{
 private double amount;
 public InsufficientFundsException(double amount)
 {
 this.amount = amount;
 } 
 public double getAmount()
 {
 return amount;
 }
}
葎阻婢幣泌採聞喘厘断徭協吶議呟械窃?
壓和中議CheckingAccount 窃嶄淫根匯倖withdraw()圭隈砺竃匯倖InsufficientFundsException呟械。
// File Name CheckingAccount.java
import java.io.*;
 
public class CheckingAccount
{
 private double balance;
 private int number;
 public CheckingAccount(int number)
 {
 this.number = number;
 }
 public void deposit(double amount)
 {
 balance += amount;
 }
 public void withdraw(double amount) throws
 InsufficientFundsException
 {
 if(amount <= balance)
 {
 balance -= amount;
 }
 else
 {
 double needs = amount - balance;
 throw new InsufficientFundsException(needs);
 }
 }
 public double getBalance()
 {
 return balance;
 }
 public int getNumber()
 {
 return number;
 }
}
和中議BankDemo殻會幣袈阻泌採距喘CheckingAccount窃議deposit() 才withdraw()圭隈。
// File Name BankDemo.java
public class BankDemo
{
 public static void main(String [] args)
 {
 CheckingAccount c = new CheckingAccount(101);
 System.out.println("Depositing 500ドル...");
 c.deposit(500.00);
 try
 {
 System.out.println("
Withdrawing 100ドル...");
 c.withdraw(100.00);
 System.out.println("
Withdrawing 600ドル...");
 c.withdraw(600.00);
 }catch(InsufficientFundsException e)
 {
 System.out.println("Sorry, but you are short $"
 + e.getAmount());
 e.printStackTrace();
 }
 }
}
園咎貧中眉倖猟周?旺塰佩殻會BankDemo?誼欺潤惚泌和侭幣?
Depositing 500ドル...
Withdrawing 100ドル...
Withdrawing 600ドル...
Sorry, but you are short 200ドル.0
InsufficientFundsException
 at CheckingAccount.withdraw(CheckingAccount.java:25)
 at BankDemo.main(BankDemo.java:13)

宥喘呟械?

壓Java嶄協吶阻曾嶽窃侏議呟械才危列。
  • JVM(Java倡亭字)呟械?喇JVM砺竃議呟械賜危列。箭泌?NullPointerException窃?ArrayIndexOutOfBoundsException窃?ClassCastException窃。
  • 殻會雫呟械?喇殻會賜宀API殻會砺竃議呟械。箭泌IllegalArgumentException窃?IllegalStateException窃。
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /