Results 1 to 2 of 2

Thread: Execptions

  1. #1
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Execptions

    I am trying to learn how exceptions work. I have created the below code which handles exceptions. It is a pretty silly example that I am using to illustrate my issue.

    I have created two similar methods (divide) withing two different classes. One of the methods throws an ArithmeticException whereas the other throws MyException.

    Netbeans warns me of an "Unreported Exception MyException" at "int j = ex2.divide()". Why is this error reported here and not at "int j = ex.divide()" ? The only difference between "int j = ex.divide()" and "int j = ex2.divide()" is the thrown exception, no?

    ------code---------

    class MyException extends Exception{
    MyException(){};
    MyException(String message){
    System.out.println(message);

    }
    }



    class Exceptions2 {
    int num;
    Exceptions2(int num) {

    this.num=num;
    }

    int divide() throws MyException{

    if (num==0) throw new MyException("I am throwing my exception");
    return 100/num;
    }

    }


    public class Exceptions {
    int num;
    Exceptions(int num) {

    this.num=num;
    }

    int divide() throws ArithmeticException{
    return 100/num;
    }


    public static void main (String[] args) {



    Exceptions ex = new Exceptions(0);
    Exceptions2 ex2 = new Exceptions2(0);

    int j = ex.divide(); // this works okay. It is not underlined by
    // Netbeans. Event though this is very similar to
    //int k = ex2.divide(); netbeans does not report
    //an error here

    // netbeans underlines the below line showing: "Unreported
    //Exception MyException; must be caught or declared to be thrown"
    //Why isn't this error reported at int j = ex.divide();?

    int k = ex2.divide(); // this does not work ok.

    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you wrap it in a try catch clause?

    Java Code:
    try{
      int k = ex2.divide();
    } catch(MyException me){
      me.printStackTrace();
    }
    I believe the first one works because arithmetic exception is an unchecked exception, and I believe it will automatically be thrown for you.(you dont have to throw it in the divide method)

    Java Code:
    public class Exceptions{
    	int num;
    	public Exceptions(int num){
    		this.num = num;
    	}
    	int divide(){
    		return 100 / num;
    	}
    	public static void main(String[] args){
    		Exceptions ex = new Exceptions(0);
    		ex.divide();
    	}
    }
    This code doesn't declare that it throws an arithmetic exception. Yet it still does throw one, I suggest you read up on checked and unchecked exceptions.
    Last edited by sunde887; 04-19-2011 at 12:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •