抛出异常时简单的Java程序中断

时间:2016-10-17 18:38:18

标签: java exception runtimeexception

我有一个简单的程序,抛出异常,我理解输出,但不是我得到的异常。 这是代码:

//Q1.java
import java.io.*;
import java.util.*;

public class Q1{
  public static void main(String args[]){
    Q1 q1=new Q1();
    Q2 q2=new Q2();
    try{
      System.out.println(q2.method(q1));
      System.out.println(q2.method(q2));
    }catch(QE1 e){
      System.out.println("exception 1");
    }finally{
      System.out.println("finally");
    }
  }

  Object method(Q1 q) throws QE1{ 
    if(q instanceof Q1){
      System.out.println("method");
    }
    else {
      throw new QE2();
    }
    return 1;
  }
}

class Q2 extends Q1{

  Object method(Q1 q) throws QE1{
    if(errorCheck()&& q instanceof Q2){
      throw new QE2("error 2");
    }else if(q instanceof Q2){
      throw new QE1();
    }else{
      return new String("abc");
    }
  }

  boolean errorCheck(){
    return true; 
  }

}

class QE1 extends Throwable{
  public QE1(){
    System.out.println("qe1 - 1");
  }
  public QE1(String s){
    super(s);
    System.out.println("qe1 - 2");
  }
}

class QE2 extends RuntimeException {

  public QE2(){
    System.out.println("qe2 - 1");
  }
  public QE2(String s){
    this();
    System.out.println("qe2 - 2");
  }

}

输出是:

abc
qe2 - 1
qe2 - 2
finally
QE2
    at Q2.method(Q1.java:33)
    at Q1.main(Q1.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

然后程序中断,但我不确定原因。 有问题的行是QE2的构造函数中的行 我有点假设,因为QE2是RuntimeException的子类,因此调用了RuntimeException的构造函数,因此程序会中断。
是这种情况还是其他什么?

1 个答案:

答案 0 :(得分:1)

您的代码的输出/行为没有任何意外。

public class HomeFragment extends Fragment implements OnItemClickListener, OnClickListener, MainActivity.OnLocationListener {

private View viewInflate;
DisplayImageOptions options;
ArrayList<Store> storeList;
ArrayList<News> newsList;
MGAsyncTask task;
Queries q;
private Store store;
Random rand = new Random();


public HomeFragment() { }

@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    viewInflate = inflater.inflate(R.layout.fragment_home, null);
    return viewInflate;
}



@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

@Override
public void onDestroy() {

    // TODO Auto-generated method stub
    super.onDestroy();
    if(task != null)
        task.cancel(true);
}



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(UIConfig.SLIDER_PLACEHOLDER)
            .showImageForEmptyUri(UIConfig.SLIDER_PLACEHOLDER)
            .showImageOnFail(UIConfig.SLIDER_PLACEHOLDER)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    storeList = new ArrayList<Store>();
    newsList = new ArrayList<News>();

    MainActivity main = (MainActivity) getActivity();

    timer();
    main.showSwipeProgress();
    main.getDebugKey();

    q = main.getQueries();
    if(MGUtilities.isLocationEnabled(getActivity())) {
        if(MainActivity.location == null) {
            main.setOnLocationListener(this);
        }
        else {
            Handler h = new Handler();
            h.postDelayed(new Runnable() {

                @Override
                public void run() {
                    getData();
                }
            }, Config.DELAY_SHOW_ANIMATION);
        }
    }
    else {
        main.setOnLocationListener(this);
    }
}


public void getData() {
    task = new MGAsyncTask(getActivity());

    task.setMGAsyncTaskListener(new OnMGAsyncTaskListener() {

        @Override
        public void onAsyncTaskProgressUpdate(MGAsyncTask asyncTask) {

        }

        @Override
        public void onAsyncTaskPreExecute(MGAsyncTask asyncTask) {
            asyncTask.dialog.hide();

        }

        @Override
        public void onAsyncTaskPostExecute(MGAsyncTask asyncTask) {
            // TODO Auto-generated method stub

            if(!MGUtilities.hasConnection(getActivity())) {
                ArrayList<Store> stores = q.getStoresFeatured();
                ArrayList<News> news = q.getNews();
                if (Config.HOME_STORE_FEATURED_COUNT != -1 && Config.RANK_STORES_ACCORDING_TO_NEARBY) {
                    Log.v("myApp", "STORE SLIDER THING - STORE SLIDER THING - STORE SLIDER THING - STORE SLIDER THING - STORE SLIDER THING - STORE SLIDER THING - " );

                    int storeCount = stores.size() < Config.HOME_STORE_FEATURED_COUNT ?
                            stores.size() : Config.HOME_STORE_FEATURED_COUNT;

                    if (MainActivity.location != null) {
                        for (Store store : stores) {
                            Location locStore = new Location("Store");
                            locStore.setLatitude(store.getLat());
                            locStore.setLongitude(store.getLon());
                            double userDistanceFromStore = MainActivity.location.distanceTo(locStore) / 1000;
                            store.setDistance(userDistanceFromStore);
                        }

                        Collections.sort(stores, new Comparator<Store>() {
                            @Override
                            public int compare(Store store, Store t1) {
                                if (store.getDistance() < t1.getDistance())
                                    return -1;
                                if (store.getDistance() > t1.getDistance())
                                    return 1;
                                return 0;
                            }
                        });
                    }
                    storeList = new ArrayList<Store>();
                    for (int x = 0; x < storeCount; x++) {

                        storeList.add(stores.get(x));

                    }
                } else {
                    storeList = stores;
                }

                if (Config.HOME_NEWS_COUNT != -1 && Config.RANK_STORES_ACCORDING_TO_NEARBY) {
                    Log.v("myApp", "NEWS SLIDER THING - NEWS SLIDER THING - NEWS SLIDER THING - NEWS SLIDER THING - NEWS SLIDER THING - NEWS SLIDER THING - " );

                    int newsCount = news.size() < Config.HOME_NEWS_COUNT ? news.size() : Config.HOME_NEWS_COUNT;
                    newsList = new ArrayList<News>();
                    for (int x = 0; x < newsCount; x++) {
                        newsList.add(news.get(x));
                    }
                } else {
                    newsList = news;
                }
            }




            createSlider();


            showList();

            MainActivity main = (MainActivity) getActivity();
            main.hideSwipeProgress();
        }

        @Override
        public void onAsyncTaskDoInBackground(MGAsyncTask asyncTask) {
            // TODO Auto-generated method stub
            TextView tvTitleHeaderr = (TextView) viewInflate.findViewById(R.id.all);

            if( MGUtilities.hasConnection(getActivity()) && MainActivity.location != null) {

                try {
                    UserAccessSession accessSession = UserAccessSession.getInstance(getActivity());
                    String strUrl = "";
                    if(accessSession.getFilterDistance() == 0) {
                        Log.v("myApp", "STRURL - STRURL - STRURL - STRURL - STRURL - STRURL - STRURL - STRURL - STRURL - STRURL - " );
                        if(MGUtilities.hasConnection(getActivity())) {

                            try {

                                strUrl = String.format("%s?api_key=%s",
                                        Config.GET_CATEGORIES_JSON_URL,
                                        Config.API_KEY);

                                DataParser parser = new DataParser();
                                Data data = parser.getData(strUrl);

                                MainActivity main = (MainActivity) getActivity();
                                if (main == null)
                                    return;

                                Queries q = main.getQueries();
                                if (data == null)
                                    return;

                                q.deleteTable("categories");
                                if (data.getCategories() != null && data.getCategories().size() > 0) {
                                    Log.v("myApp", "CATEGORYS - CATEGORYS - CATEGORYS - CATEGORYS - CATEGORYS - CATEGORYS - CATEGORYS - CATEGORYS - " );

                                    for (Category cat : data.getCategories()) {
                                        q.insertCategory(cat);
                                    }
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        strUrl = String.format("%s?api_key=%s&lat=%f&lon=%f&radius=%f&news_count=%d&featured_count=%d&default_store_count_to_find_distance=%d",
                                Config.GET_HOME_STORES_NEWS_JSON_URL,
                                Config.API_KEY,
                                MainActivity.location.getLatitude(),
                                MainActivity.location.getLongitude(),
                                accessSession.getFilterDistance(),
                                Config.HOME_NEWS_COUNT,
                                Config.HOME_FEATURED_COUNT,
                                Config.DEFAULT_STORE_COUNT_TO_FIND_DISTANCE);
                    }
                    else {

                        strUrl = String.format("%s?api_key=%s&lat=%f&lon=%f&radius=%f&news_count=%d&default_store_count_to_find_distance=%d",
                                Config.GET_HOME_STORES_NEWS_JSON_URL,
                                Config.API_KEY,
                                MainActivity.location.getLatitude(),
                                MainActivity.location.getLongitude(),
                                accessSession.getFilterDistance(),
                                Config.HOME_NEWS_COUNT,
                                Config.DEFAULT_STORE_COUNT_TO_FIND_DISTANCE);
                    }

                    DataParser parser = new DataParser();
                    Data data = parser.getData(strUrl);
                    MainActivity main = (MainActivity) getActivity();
                    if (main == null)
                        return;

                    Queries q = main.getQueries();
                    if (data == null)
                        return;

                    if(Config.AUTO_ADJUST_DISTANCE) {
                        if(data.getMax_distance() > 0) {
                            UserAccessSession.getInstance(getActivity()).setFilterDistanceMax(data.getMax_distance());
                        }

                        if(UserAccessSession.getInstance(getActivity()).getFilterDistance() == 0) {
                            UserAccessSession.getInstance(getActivity()).setFilterDistance(data.getDefault_distance());
                        }
                    }



                    if (data.getStores() != null && data.getStores().size() > 0) {

                        Log.v("myApp", "ENTERED THE IF - ENTERED THE IF - ENTERED THE IF - ENTERED THE IF - ENTERED THE IF" );

                        int storeCount = data.getStores().size() < Config.HOME_STORE_FEATURED_COUNT ?
                                data.getStores().size() : Config.HOME_STORE_FEATURED_COUNT;

                        int x = 0;
                        for (Store store : data.getStores()) {
                            Location locStore = new Location("Store");
                            locStore.setLatitude(store.getLat());
                            locStore.setLongitude(store.getLon());
                            double userDistanceFromStore = MainActivity.location.distanceTo(locStore) / 1000;
                            store.setDistance(userDistanceFromStore);
                            if (userDistanceFromStore < 50){
                            Log.v("myApp", "STORE - STORE - STORE - STORE - STORE - STORE - STORE - STORE - STORE - STORE - STORE- STORE" );
                            q.deleteStore(store.getStore_id());
                            q.insertStore(store);


                            if(x < storeCount) {
                                Log.v("myApp", "if(x < storeCount) - if(x < storeCount)- if(x < storeCount)- if(x < storeCount)- if(x < storeCount)- if(x < storeCount)" );

                                if (store.getFeatured() == 3 ) {
                                    storeList.add(store);
                                    x += 1;

                                }


                            }

                            if (store.getPhotos() != null && store.getPhotos().size() > 0) {
                                Log.v("myApp", "PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS - PHOTOS" );
                                for (Photo photo : store.getPhotos()) {
                                    q.deletePhoto(photo.getPhoto_id());
                                    q.insertPhoto(photo);
                                }
                            }
                        }}


                    }

                    if (data.getNews() != null && data.getNews().size() > 0) {
                        Log.v("myApp", "NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS - NEWS" );
                        int newsCount = data.getNews().size() < Config.HOME_NEWS_COUNT
                                ? data.getNews().size() : Config.HOME_NEWS_COUNT;

                        int x = 0;
                        for (News news : data.getNews()) {
                            q.deleteNews(news.getNews_id());
                            q.insertNews(news);

                            if(x < newsCount) {
                                newsList.add(news);
                                x += 1;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                    }
        }
    });
    task.execute();
}

@Override
public void onDestroyView()  {

    super.onDestroyView();
    if (viewInflate != null) {
        ViewGroup parentViewGroup = (ViewGroup) viewInflate.getParent();
        if (parentViewGroup != null) {
            MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
            slider.pauseSliderAnimation();
            parentViewGroup.removeAllViews();
        }
    }

    handler.removeCallbacks(runnable);
    if(task != null)
        task.cancel(true);
}
Handler handler = new Handler();
int apple = 0;

Runnable runnable = new Runnable() {
    public void run() {
        if(apple < 5) {
            timer();
        }
    }
};

public void timer()
{

    int randomNum = 1 + rand.nextInt((50 - 1) + 1);
    TextView tvTitleHeader = (TextView) viewInflate.findViewById(R.id.tvTitleeee);
    if (randomNum == 1 ){
        tvTitleHeader.setText(getString(R.string.A1));
    }
    if (randomNum == 2 ){
        tvTitleHeader.setText(getString(R.string.A2));
    }
    if (randomNum == 3 ){
        tvTitleHeader.setText(getString(R.string.A3));
    }
    if (randomNum == 4 ){
        tvTitleHeader.setText(getString(R.string.A4));
    }
    if (randomNum == 5 ){
        tvTitleHeader.setText(getString(R.string.A5));
    }
    if (randomNum == 6 ){
        tvTitleHeader.setText(getString(R.string.A6));
    }
    if (randomNum == 7 ){
        tvTitleHeader.setText(getString(R.string.A7));
    }
    if (randomNum == 8 ){
        tvTitleHeader.setText(getString(R.string.A8));
    }
    if (randomNum == 9 ){
        tvTitleHeader.setText(getString(R.string.A9));
    }
    if (randomNum == 10 ){
        tvTitleHeader.setText(getString(R.string.A10));
    }
    if (randomNum == 11 ){
        tvTitleHeader.setText(getString(R.string.A11));
    }
    if (randomNum == 12 ){
        tvTitleHeader.setText(getString(R.string.A12));
    }
    if (randomNum == 13 ){
        tvTitleHeader.setText(getString(R.string.A13));
    }
    if (randomNum == 14 ){
        tvTitleHeader.setText(getString(R.string.A14));
    }
    if (randomNum == 15 ){
        tvTitleHeader.setText(getString(R.string.A15));
    }
    if (randomNum == 16 ){
        tvTitleHeader.setText(getString(R.string.A16));
    }
    if (randomNum == 17 ){
        tvTitleHeader.setText(getString(R.string.A17));
    }
    if (randomNum == 18 ){
        tvTitleHeader.setText(getString(R.string.A18));
    }
    if (randomNum == 19 ){
        tvTitleHeader.setText(getString(R.string.A19));
    }
    if (randomNum == 20 ){
        tvTitleHeader.setText(getString(R.string.A20));
    }
    if (randomNum == 21 ){
        tvTitleHeader.setText(getString(R.string.A21));
    }
    if (randomNum == 22 ){
        tvTitleHeader.setText(getString(R.string.A22));
    }
    if (randomNum == 23 ){
        tvTitleHeader.setText(getString(R.string.A23));
    }
    if (randomNum == 24 ){
        tvTitleHeader.setText(getString(R.string.A24));
    }
    if (randomNum == 25 ){
        tvTitleHeader.setText(getString(R.string.A25));
    }
    if (randomNum == 26 ){
        tvTitleHeader.setText(getString(R.string.A26));
    }
    if (randomNum == 27 ){
        tvTitleHeader.setText(getString(R.string.A27));
    }
    if (randomNum == 28 ){
        tvTitleHeader.setText(getString(R.string.A28));
    }
    if (randomNum == 29 ){
        tvTitleHeader.setText(getString(R.string.A29));
    }
    if (randomNum == 30 ){
        tvTitleHeader.setText(getString(R.string.A30));
    }
    if (randomNum == 31 ){
        tvTitleHeader.setText(getString(R.string.A31));
    }
    if (randomNum == 32 ){
        tvTitleHeader.setText(getString(R.string.A32));
    }
    if (randomNum == 33 ){
        tvTitleHeader.setText(getString(R.string.A33));
    }
    if (randomNum == 34 ){
        tvTitleHeader.setText(getString(R.string.A34));
    }
    if (randomNum == 35 ){
        tvTitleHeader.setText(getString(R.string.A35));
    }
    if (randomNum == 36 ){
        tvTitleHeader.setText(getString(R.string.A36));
    }
    if (randomNum == 37 ){
        tvTitleHeader.setText(getString(R.string.A37));
    }
    if (randomNum == 38 ){
        tvTitleHeader.setText(getString(R.string.A38));
    }
    if (randomNum == 39 ){
        tvTitleHeader.setText(getString(R.string.A39));
    }
    if (randomNum == 40 ){
        tvTitleHeader.setText(getString(R.string.A40));
    }
    if (randomNum == 41 ){
        tvTitleHeader.setText(getString(R.string.A41));
    }
    if (randomNum == 42 ){
        tvTitleHeader.setText(getString(R.string.A42));
    }
    if (randomNum == 43 ){
        tvTitleHeader.setText(getString(R.string.A43));
    }
    if (randomNum == 44 ){
        tvTitleHeader.setText(getString(R.string.A44));
    }
    if (randomNum == 45 ){
        tvTitleHeader.setText(getString(R.string.A45));
    }
    if (randomNum == 46 ){
        tvTitleHeader.setText(getString(R.string.A46));
    }
    if (randomNum == 47 ){
        tvTitleHeader.setText(getString(R.string.A47));
    }
    if (randomNum == 48 ){
        tvTitleHeader.setText(getString(R.string.A48));
    }
    if (randomNum == 49 ){
        tvTitleHeader.setText(getString(R.string.A49));
    }
    if (randomNum == 50 ){
        tvTitleHeader.setText(getString(R.string.A50));
    }


    handler.postDelayed(runnable, 15000);
    apple++;
}
private void showList() {
    ListView listView = (ListView) viewInflate.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    MGListAdapter adapter = new MGListAdapter(
            getActivity(), newsList.size(), R.layout.twitter_dialog);

    adapter.setOnMGListAdapterAdapterListener(new OnMGListAdapterAdapterListener() {

        @Override
        public void OnMGListAdapterAdapterCreated(MGListAdapter adapter, View v,
                                                  int position, ViewGroup viewGroup) {
            // TODO Auto-generated method stub
            News news = newsList.get(position);
            MGImageView imgViewPhoto = (MGImageView) v.findViewById(R.id.imgViewPhoto);
            imgViewPhoto.setCornerRadius(0.0f);
            imgViewPhoto.setBorderWidth(UIConfig.BORDER_WIDTH);
            imgViewPhoto.setBorderColor(getResources().getColor(UIConfig.THEME_BLACK_COLOR));

            if(news.getPhoto_url() != null) {
                MainActivity.getImageLoader().displayImage(news.getPhoto_url(), imgViewPhoto, options);
            }
            else {
                MainActivity.getImageLoader().displayImage(null, imgViewPhoto, options);
            }

            imgViewPhoto.setTag(position);
            Spanned name = Html.fromHtml(news.getNews_title());
            Spanned address = Html.fromHtml(news.getNews_content());

            TextView tvTitle = (TextView) v.findViewById(R.id.tvTitle);
            tvTitle.setText(name);

            TextView tvSubtitle = (TextView) v.findViewById(R.id.tvSubtitle);
            tvSubtitle.setText(address);

            String date = DateTimeHelper.getStringDateFromTimeStamp(news.getCreated_at(), "MM/dd/yyyy" );
            TextView tvDate = (TextView) v.findViewById(R.id.tvDate);
            tvDate.setText(date);
        }
    });
    listView.setAdapter(adapter);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long resId) {
    // TODO Auto-generated method stub
    MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
    slider.stopSliderAnimation();

    News news = newsList.get(pos);
    Intent i = new Intent(getActivity(), NewsDetailActivity.class);
    i.putExtra("news", news);
    getActivity().startActivity(i);
}

// Create Slider
private void createSlider() {
    if(storeList != null && storeList.size() == 0 && newsList != null && newsList.size() == 0) {
        MGUtilities.showNotifier(this.getActivity(), MainActivity.offsetY, R.string.failed_data);
        return;

    }

    final MainActivity main = (MainActivity) getActivity();
    final Queries q = main.getQueries();

    MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
    if (storeList.size() < 1) {
        FrameLayout layout = (FrameLayout)viewInflate.findViewById(R.id.framelayout100);
        layout.setVisibility(View.GONE);
        ImageView layoutt = (ImageView) viewInflate.findViewById(R.id.framelayout1001);
        layoutt.setVisibility(View.GONE);
    }

    slider.setMaxSliderThumb(storeList.size());
    MGSliderAdapter adapter = new MGSliderAdapter(
            R.layout.slider_entry, storeList.size(), storeList.size());

    adapter.setOnMGSliderAdapterListener(new OnMGSliderAdapterListener() {

        @Override
        public void onOnMGSliderAdapterCreated(MGSliderAdapter adapter, View v,
                                               int position) {
            // TODO Auto-generated method stub
            final Store entry = storeList.get(position);
            Photo p = q.getPhotoByStoreId(entry.getStore_id());
            ImageView imageViewSlider = (ImageView) v.findViewById(R.id.imageViewSlider);
            if(p != null) {
                MainActivity.getImageLoader().displayImage(p.getPhoto_url(), imageViewSlider, options);
            }
            else {
                imageViewSlider.setImageResource(UIConfig.SLIDER_PLACEHOLDER);
            }

            imageViewSlider.setTag(position);
            imageViewSlider.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(getActivity(), DetailActivity.class);
                    i.putExtra("store", entry);
                    getActivity().startActivity(i);
                }
            });

            Spanned name = Html.fromHtml(entry.getStore_name());
            Spanned address = Html.fromHtml(entry.getIdade());
            Spanned hour = Html.fromHtml(entry.getHora());

            TextView tvTitle = (TextView) v.findViewById(R.id.tvTitle);
            tvTitle.setText(name);

            TextView tvSubtitle = (TextView) v.findViewById(R.id.tvSubtitle);
            tvSubtitle.setText(" "+ address);

            TextView tvHour = (TextView) v.findViewById(R.id.tvHour);
            tvHour.setText(" "+hour);

            TextView tvDistance = (TextView) v.findViewById(R.id.tvDistance);
            tvDistance.setVisibility(View.INVISIBLE);
            if(MGUtilities.isLocationEnabled(getActivity()) && MainActivity.location != null) {
                if(entry.getDistance() != -1) {
                    tvDistance.setVisibility(View.VISIBLE);
                    double miles = entry.getDistance();
                    String format = String.format(
                            "%.2f %s", miles,
                            MGUtilities.getStringFromResource(getActivity(), R.string.km));
                    tvDistance.setText(format);
                }
                else {
                    tvDistance.setText(R.string.empty_distance);
                }
            }
        }
    });

    slider.setOnMGSliderListener(new OnMGSliderListener() {

        @Override
        public void onItemThumbSelected(MGSlider slider, ImageView[] buttonPoint,
                                        ImageView imgView, int pos) { }

        @Override
        public void onItemThumbCreated(MGSlider slider, ImageView imgView, int pos) { }


        @Override
        public void onItemPageScrolled(MGSlider slider, ImageView[] buttonPoint, int pos) { }

        @Override
        public void onItemMGSliderToView(MGSlider slider, int pos) { }

        @Override
        public void onItemMGSliderViewClick(AdapterView<?> adapterView, View v,
                                            int pos, long resid) { }

        @Override
        public void onAllItemThumbCreated(MGSlider slider, LinearLayout linearLayout) { }

    });
    slider.setOffscreenPageLimit(storeList.size() - 1);
    slider.setAdapter(adapter);
    slider.setActivity(this.getActivity());
    slider.setSliderAnimation(3000);
    slider.resumeSliderAnimation();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
    slider.stopSliderAnimation();
    switch(v.getId()) { }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
    slider.resumeSliderAnimation();
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    MGSlider slider = (MGSlider) viewInflate.findViewById(R.id.slider);
    slider.pauseSliderAnimation();
}

@Override
public void onLocationChanged(Location prevLoc, Location currentLoc) {
    MainActivity main = (MainActivity) getActivity();
    main.setOnLocationListener(null);
    getData();
}
public static void displayPromptForEnablingGPS(
        final Activity activity)
{
    final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
    final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
    final String message = "Não foi possível localiza-lo."
            + " Por favor habilite seu GPS para encontrar"
            + " as acompanhantes próximas de você.";


    builder.setMessage(message)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int id) {
                            activity.startActivity(new Intent(action));
                            System.exit(0);
                            d.dismiss();
                        }
                    });

    builder.create().show();


}
}

您在public static void main(String args[]) { Q1 q1 = new Q1(); Q2 q2 = new Q2(); try { // this prints "abc" and no exception is thrown System.out.println(q2.method(q1)); // this throws a new QE2(), which prints // this prints "qe2 - 1" followed by "qe2 - 2" System.out.println(q2.method(q2)); } catch(QE1 e) { System.out.println("exception 1"); } finally { // your code ends up here, which prints "finally" System.out.println("finally"); } } 方法的QE2块中抛出了try类型的例外情况。但是,您实际上从未捕获过这种类型的异常,因为您只捕获main。因此,您最终会在QE1区块中打印finally

如果您想捕获所有异常,那么您可以在"finally"阻止之前使用catch (Exception e)