How to propagate an exception in java

后端 未结 4 1804
無奈伤痛
無奈伤痛 2020-12-19 00:30

I am a C programmer and just learning some java recently because I am developing one android application. Currently I am in a situation. Following is the one.



        
4条回答
  •  感情败类
    2020-12-19 01:21

    I always do it like this :

    public void MyMethod() throws Exception
    {
        //code here
        if(something is wrong)
            throw new Exception("Something wrong");
    }
    

    then when you call the function

    try{
        MyMethod();
       }catch(Exception e){
        System.out.println(e.getMessage());
    }
    

提交回复
热议问题