Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:48:03+00:00 2026-06-12T22:48:03+00:00

What is the problem here? I am just simply trying to catch a custom

  • 0

What is the problem here? I am just simply trying to catch a custom exception depending on a if statement in my constructor, the entire code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package question3_test;

/**
 *
 * @author jackandjill
 */
public class Triangle2 
{
    private int side1, side2, side3;
    private  double area;
    private  String message;    

    public  int getSide1()
    {
        return side1;
    }

    public void setSide1(int s1)
    {
        side1 = s1;
    }

    public  int getSide2()
    {
        return side2;
    }

    public void setSide2(int s2)
    {
        side2 = s2;
    }

    public  int getSide3()
    {
        return side3;
    }

    public void setSide3(int s3)
    {
        side3 = s3;
    }

   public Triangle2(int a,int b,int c) throws InvalidValueException
   {
    setSide1(a);
    setSide2(b);
    setSide3(c);

    try
    {
         if (!(side1 + side2 > side3) && (side2 + side3 > side1)&& (side1 + side3 > side2))

                 throw new InvalidValueException("Invalid Value");
    }
    catch (InvalidValueException excep)
    {
        message = excep.getMessage();
        //throw new InvalidValueException(message);
    }

    public double findArea(int side_1, int side_2, int side_3)
    {
        int s, a, b,c;
        a = side_1;
        b = side_2;
        c = side_3;
        s = ((a + b + c)/2);
        area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
        //area =
        return area;
    }
   }
}

The problem is at my catch statement, it is underlined in red and says never thrown in the corresponding try statement

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:48:05+00:00Added an answer on June 12, 2026 at 10:48 pm

    You can’t throw an Exception class like in the below code: –

    throw InvalidValueException;
    

    You have to make a new instance of your exception, and then throw it: –

    throw new InvalidValueException();
    

    But, still there is some logic problem in your code. You have declared your Exception to be throws, but at the same time, you are handling it in the catch block itself. Why would you do that?

    UPDATE: –

    As per your modified post: –

    try
    {
       if (!(side1 + side2 > side3) && (side2 + side3 > side1) && 
            (side1 + side3 > side2))
    
           message = "Invalid Triangle";
    
    } catch (InvalidValueException e) {
        message = "Invalid Traingle";
    }
    

    Since you are no-where throwing any Exception in your try block, the catch block will complain about that..

    If you want to throw an exception and catch it in place, use it like this: –

    public Triangle2(int a,int b,int c) throws InvalidValueException
    
          if (!(side1 + side2 > side3) && (side2 + side3 > side1) && 
                (side1 + side3 > side2))
    
               throw new InvalidValueException("Invalid Value")
    }
    

    And suppose you are using it from your main method, you should have a try-catch block there, to handle the declared exception in constructor.: –

    public static void main(String[] args) {
        Triangle triangle = null;
        try {
             triangle = new Triangle(2, 3, 4);
        } catch (InvalidValueException e) {
            e.printStackTrace();
        }
    }
    

    If you handle the exception in your method only, you should not declare it in your method declaration.. But if you are not handling it, then you must declare that exception to be thrown.

    An exception should be either handled, or declared to be thrown (In which case, you should actually throw that exception), but never both

    See This tutorial for more about Exception: – http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE Turns out I'm just tired. There isn't any problem here sorry for wasting
I have just been helped on a problem I have here . var win
Interesting problem I have here, usually I just round up/down respectively for my needs
Here's a problem I've had recently that just HAS to be a common pain
I don't have a problem, I'm just curious. Here is a part of my
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
I have a problem here im trying to upload a file first time it
I'm fed up with this. I've been trying to just get a grip on
been banging my head on the table over this all night. I'm simply trying
I simply cannot understand what is happening here. The problem is important for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.