Catching an Exception from a called Method

前端 未结 4 717
情歌与酒
情歌与酒 2021-01-24 06:30

This is something that\'s been bugging me for a while with regards to Program Flow.

I wanted to know if it\'s possible to catch an error from a Method in order to stop i

4条回答
  •  孤独总比滥情好
    2021-01-24 06:52

    You put method01 and method02 in to same try block:

    public class MyClass {
    
        public static void main(String[] args) {
            try {
                // This method catches an exception and stops running.
                method01();
                // This method will not continue if method01 have exception.
                method02();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    // declare method01, method02, others...
    
    }
    

    Notice: You have mistakes at the end of code block ( }; }; )

提交回复
热议问题