返回列表 發帖
  1. import java.util.*;
  2. public class ch52
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                
  7.                 int x,y;
  8.                 for(int i=1;i<=3;i++)
  9.                 {
  10.                         try
  11.                         {
  12.                                 Scanner s=new Scanner(System.in);
  13.                                 System.out.print("輸入分子: ");
  14.                                 x=s.nextInt();
  15.                                 System.out.print("輸入分母:");
  16.                                 y=s.nextInt();
  17.                                 System.out.print(x+"/"+y+"="+(x/y));
  18.                                 return;
  19.                         }
  20.                         catch(InputMismatchException e)
  21.                         {
  22.                                 System.out.println("格式錯誤!");
  23.                         }
  24.                         catch(ArithmeticException e)
  25.                         {
  26.                                 System.out.println("運算錯誤!");
  27.                         }
  28.                         if(i==3)
  29.                                 System.out.println("程式跳出");
  30.                 }
  31.         }
  32. }
複製代碼

TOP

  1. #include <iostream>
  2. #include"point.h"
  3. #include <exception>
  4. using namespace std;
  5. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  6. struct ooops : std::exception {
  7.   const char* what() const throw() {return "Ooops!\n";}
  8. };
  9. void print(exception& ex)
  10. {
  11.         cout<<ex.what();
  12. }

  13. int main() {
  14.        
  15.         ooops e;

  16.   std::exception* p = &e;
  17.   cout<<p->what();
  18.         try
  19.         {
  20.                 print(*p);
  21.                 throw *p;
  22.         }
  23.         catch(exception& ex)
  24.         {
  25.                 cout<<ex.what();
  26.         }
  27. }
複製代碼

TOP

返回列表