-3

I am a beginner so please don't use hard or advanced terminologies. I tried using throws in the method and then removing it and noticed that it really does not make any difference what so ever if I'm handling it in the main when invoking the method.

public Triangle(int side1, int side2, int side3) throws Exception{
            if (side1 <= 0 || side2 <= 0 || side3 <= 0)
                throw new IllegalArgumentException("Sides can only be positive numbers");
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;}





public static void main (String  args []) {
            try {
            Triangle obj = new Triangle (0,1,1) ;
            }catch (Exception e) {
                System.out.println("Caught");
            }

I once even tried dividing by 0 in the method without using throws and was still able to handle the exception in the main if I used try and catch when invoking the method.

When is it necessary to use throws with a function if I want to handle an exception in someplace else (ex. main method)?

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
Faisal
  • 35
  • 3
  • When you are going to throw an exception that otherwise would not be thrown – SteelToe Apr 02 '18 at 17:11
  • Possible duplicate of [Throwing exceptions in Java](https://stackoverflow.com/questions/528417/throwing-exceptions-in-java) – guillaume girod-vitouchkina Apr 02 '18 at 17:11
  • IllegalArgumentException is a runtime exception. These *can* be, but does not *need* to be declared to be thrown in the method declaration. Try throwing a checked Exception without declaring it and you will see a problem... – Zippy Apr 02 '18 at 17:13
  • 1
    `IllegalArgumentException` is a subclass of `RuntimeException`, which does not need to be declared. – Johannes Kuhn Apr 02 '18 at 17:14
  • 1
    Possible duplicate of [Java: checked vs unchecked exception explanation](https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – luk2302 Apr 02 '18 at 17:15
  • Please read up on what exceptions are, when they are useful and so on. – luk2302 Apr 02 '18 at 17:17

2 Answers2

0

Prologue: Exceptions are of two types Unchecked or RuntimeException and Checked Exception. Every exception that is subclass of RuntimeException is unchecked every thing else is checked. Dividing by zero generates unchecked exception. While File not found is a checked exception.

Now lets dive into your question- A method should declare throws if you want caller to handle it. Convention is not to handle RuntimeExceptions because they can be avoided with better programming.

Also a quick code review, avoid using throws Exception, this is telling caller that you can expect any Exception, rather specify exact Exception or list of Exceptions that caller should be aware of.

noobCoder
  • 292
  • 2
  • 17
0

Throws used when writing methods, to declare that the method in question throws the specific exception. As opposed to checked exceptions. reference to throws

Lakshitha Wisumperuma
  • 1,574
  • 1
  • 10
  • 17