3

Suppose I have a class, the requirement is that "change the following code to throw checked exception if "den" is 0, and change the function to catch that exception".

public class divide {
void divide(int num, int den){
    System.out.println(""+(num/den));
}
void fun(){
    divide(4,2);
}
}

Which of the following one is the correct way to throw exception?

Option 1:

void divide(int num, int den) throws Exception{
    if(den==0){
        throw new Exception("Dividebyzero");
    }
    System.out.println(""+(num/den));
}
void fun(){
    try {
        divide(4,2);
    } catch (Exception e) {
    } 
}

Option 2: // I think this one is correct

void divide(int num, int den){
    if(den==0){
        throw new RuntimeException("Dividebyzero");
    }
    System.out.println(""+(num/den));
}
void fun(){
    try {
        divide(4,2);
    } catch (RuntimeException e) {
    } 
}

Option 3:

void divide(int num, int den) throws Exception{
    if(den==0){
        throw new RuntimeException;
    }
    System.out.println(""+(num/den));
}
void fun(){
    try {
        divide(4,2);
    } catch (Exception, RuntimeException) {
    } 
}

This problem came from one of the Java exercises. I have learned Java for several years but I am kind of confused about the try catch throw. Personally, I think that option 2 is correct because we only throw the exception once, or I am wrong?

user6119494
  • 65
  • 1
  • 7
  • Option 3 won't compile and there isn't much of a difference in option 1 or 2 – OneCricketeer Apr 19 '16 at 00:46
  • Please ignore small syntax error but look at the overall structure @cricket_007 – user6119494 Apr 19 '16 at 00:48
  • So, you're asking which subclass of Exception to use? – OneCricketeer Apr 19 '16 at 00:50
  • The question is asking which one is correct? @cricket_007 – user6119494 Apr 19 '16 at 00:52
  • I think option 1 is more correct. Denoting a method throws an exception allows you to actually throw one – OneCricketeer Apr 19 '16 at 00:58
  • but if i write instance.divide(4,0) in main and the compiler asks me to write another try/catch or throw? @cricket_007 in option 1 – user6119494 Apr 19 '16 at 01:00
  • I think option 1 is better. It's better to be explicit about the fact that the method may throw an exception. The compiler is simply asking you to either handle the exception or propogate it up. – Philip Apr 19 '16 at 01:11
  • alright @Philip ........ – user6119494 Apr 19 '16 at 01:13
  • Option 1 tells you to add a try catch? Without actually trying it, I would think Option 2 would tell you to do that – OneCricketeer Apr 19 '16 at 01:20
  • don't know how you could have been using Java for years and not understand this very VERY basic concept...doesn't even appear that you understand what a checked exception is – Scott Sosna Apr 19 '16 at 01:51
  • @ScottSosna How is that helpful? There's nothing wrong with having questions, even ones you deem so basic. Nobody's impressed by your condescension. – Philip Apr 19 '16 at 02:28
  • In general, I see too many questions raised where a single google would have clarified things if not provided the answer, and here seems nothing different: checked exceptions is one of the most basic concepts in Java, and based on the question no real effort was put down. In fact, I bet if I wasn't feeling lazy tonight, I could have found a duplicate question already asked on StackOverflow. The overriding question is such: why should I exert myself to answer when the question asker has obviously not. Very frustrating to run into those again and again. – Scott Sosna Apr 19 '16 at 02:57

1 Answers1

4

Which of the following one is the correct way to throw exception?

I wouldn't complicate the code, instead I would use the exception it already throws.

void printDivide(int num, int den) throws ArithmeticException {
    System.out.println(num / den);
}

Using a different exception is not just more complicated, it's confusing.


Lets set it is a different example, then IllagelArgumentException is a good choice for an illegal argument, like creating an array

void createArray(int size) {
    if (size < 0)
        throw IllegalArgumentException("Size must be non-negative " + size);
    this.array = new int[size];
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130