連拋2個異常,第一個是第二個的Cause,第一個第二個cause
異常只能拋一個,捕捉到再拋一個,也只是一個異常
看下面的代碼,如果你認為執行不到(如果 if 條件滿足)執行不到第2個 throwException 就錯了 (Jeallybean code)
libcore/luni/src/main/native/libcore_io_Posix.cpp#throwException
146 static void throwGaiException(JNIEnv* env, const char* functionName, int error) { 147 if (errno != 0) { 148 // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 149 // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 150 // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 151 // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 152 // http://code.google.com/p/android/issues/detail?id=15722 153 throwErrnoException(env, functionName); 154 // Deliberately fall through to throw another exception... 155 } 156 static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, 157 "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 158 static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, 159 "<init>", "(Ljava/lang/String;I)V"); 160 throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 161 }
因為這不是 native C++ 自己的exception,而是 native 送給 java 層的 exception
再看下面的函數,如果在有 Pending Exception 的情況下(env->ExceptionOccurred), Pending Exception 將成為當前Exception 的 cause
113 static void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 114 const char* functionName, int error) { 115 jthrowable cause = NULL; 116 if (env->ExceptionCheck()) { 117 cause = env->ExceptionOccurred(); 118 env->ExceptionClear(); 119 } 120 121 ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 122 if (detailMessage.get() == NULL) { 123 // Not really much we can do here. We're probably dead in the water, 124 // but let's try to stumble on... 125 env->ExceptionClear(); 126 } 127 128 jobject exception; 129 if (cause != NULL) { 130 exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 131 } else { 132 exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 133 } 134 env->Throw(reinterpret_cast<jthrowable>(exception)); 135 }
再看下面
libcore/luni/src/main/java/java/net/InetAddress.java#getAllByNameImpl
417 } catch (GaiException gaiException) { 418 // If the failure appears to have been a lack of INTERNET permission, throw a clear 419 // SecurityException to aid in debugging this common mistake. 420 // http://code.google.com/p/android/issues/detail?id=15722 421 if (gaiException.getCause() instanceof ErrnoException) { 422 if (((ErrnoException) gaiException.getCause()).errno == EACCES) { 423 throw new SecurityException("Permission denied (missing INTERNET permission?)", gaiException); 424 } 425 } 426 // Otherwise, throw an UnknownHostException. 427 String detailMessage = "Unable to resolve host \"" + host + "\": " + Libcore.os.gai_strerror(gaiException.error); 428 addressCache.putUnknownHost(host, detailMessage); 429 throw gaiException.rethrowAsUnknownHostException(detailMessage); 430 }
檢測到GaiException的時候,正好檢查 Cause 是否是 ErrnoException
java實驗:編寫程式,從鍵盤上輸入兩個數將第一個數除以第二個數並顯示結果;處理所有可可以的異常
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FuctJFrame extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel jlabel=new JLabel("請輸入兩個數:");
JLabel jlabel1=new JLabel();
JTextField jtextfield=new JTextField();
JButton jbutton=new JButton("=");
JTextField jtextfield1=new JTextField();
public FuctJFrame(){
setSize(300,300);
setTitle("求兩數相除");
setLocation(251,251);
getContentPane().setLayout(null);
jlabel.setBounds(new Rectangle(100,50,100,20));
jtextfield.setBounds(new Rectangle(100,80,100,20));
jtextfield1.setBounds(new Rectangle(100,110,100,20));
jbutton.setBounds(new Rectangle(115,140,70,20));
jlabel1.setBounds(new Rectangle(120,200, 100, 20));
jlabel1.setForeground(Color.RED);
jbutton.addActionListener(this);
getContentPane().add(jlabel);
getContentPane().add(jtextfield);
getContentPane().add(jbutton);
getContentPane().add(jtextfield1);
getContentPane().add(jlabel1);
}
public void actionPerformed(ActionEvent e){
try{
double sum;
sum=Double.parseDouble(jtextfield.getText())/Double.parseDouble(jtextfield1.getText());
jlabel1.setText(""+sum);
if(Double.parseDouble(jtextfield1.getText())==0.0)
jlabel1.setText("除數不能為0");
}catch(Exception ex){
JOptionPane.showMessageDialog(this,"請輸入數字");
}
}
public static void main(String[]arqs){
FuctJFrame x=new FuctJFra......餘下全文>>
第一個孩子苯丙氨酸異常 第二個孩子還可以這樣
有可能
苯丙酮尿症(phenylketonurics; PKU)是一種常見的氨基酸代謝病,是由於苯丙氨酸代謝途徑中的酶缺陷,使得苯丙氨酸不能轉變成為酪氨酸,導致苯丙氨酸及其酮酸蓄積並從尿中大量排出。臨床主要表現為智能低下,驚厥發作和色素減少。本病屬常染色體隱性遺傳。其發病率隨種族而異,美國約為1/14000,日本1/60000,我國1/16500。