Java exceptions, after catch it stills prints whats after





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















I am using some exceptions, but even if one is thrown and catched, it does continue output whats after the catch block.



I want my exception that is thrown, to get catched and only print out whats in the catch body, unless there is no exception and move on to the last souf.



Somehow though, when i have an exception, my catch body is printed but also the souf after it, that should not be printed.



How do i organize these exceptions?



------- a method that throws an exception



   public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!nn");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}


------- the method that handles the throw from getHeight



@Override
public double getArea() {
//if end point is not set, return -1 (error)

double area = 0;

try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
}

return area;
}


---------- here the last SOUF after the catch should not be printed, but gets printed anyways



private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());

double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}









share|improve this question

























  • I am unsure of what you are asking about, You should make your post short and straight forward

    – SamzSakerz
    Nov 17 '18 at 0:54


















-2















I am using some exceptions, but even if one is thrown and catched, it does continue output whats after the catch block.



I want my exception that is thrown, to get catched and only print out whats in the catch body, unless there is no exception and move on to the last souf.



Somehow though, when i have an exception, my catch body is printed but also the souf after it, that should not be printed.



How do i organize these exceptions?



------- a method that throws an exception



   public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!nn");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}


------- the method that handles the throw from getHeight



@Override
public double getArea() {
//if end point is not set, return -1 (error)

double area = 0;

try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
}

return area;
}


---------- here the last SOUF after the catch should not be printed, but gets printed anyways



private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());

double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}









share|improve this question

























  • I am unsure of what you are asking about, You should make your post short and straight forward

    – SamzSakerz
    Nov 17 '18 at 0:54














-2












-2








-2








I am using some exceptions, but even if one is thrown and catched, it does continue output whats after the catch block.



I want my exception that is thrown, to get catched and only print out whats in the catch body, unless there is no exception and move on to the last souf.



Somehow though, when i have an exception, my catch body is printed but also the souf after it, that should not be printed.



How do i organize these exceptions?



------- a method that throws an exception



   public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!nn");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}


------- the method that handles the throw from getHeight



@Override
public double getArea() {
//if end point is not set, return -1 (error)

double area = 0;

try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
}

return area;
}


---------- here the last SOUF after the catch should not be printed, but gets printed anyways



private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());

double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}









share|improve this question
















I am using some exceptions, but even if one is thrown and catched, it does continue output whats after the catch block.



I want my exception that is thrown, to get catched and only print out whats in the catch body, unless there is no exception and move on to the last souf.



Somehow though, when i have an exception, my catch body is printed but also the souf after it, that should not be printed.



How do i organize these exceptions?



------- a method that throws an exception



   public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!nn");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}


------- the method that handles the throw from getHeight



@Override
public double getArea() {
//if end point is not set, return -1 (error)

double area = 0;

try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
}

return area;
}


---------- here the last SOUF after the catch should not be printed, but gets printed anyways



private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());

double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}






java exception exception-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 1:36









khelwood

32.4k74466




32.4k74466










asked Nov 17 '18 at 0:51









ThesarThesar

227




227













  • I am unsure of what you are asking about, You should make your post short and straight forward

    – SamzSakerz
    Nov 17 '18 at 0:54



















  • I am unsure of what you are asking about, You should make your post short and straight forward

    – SamzSakerz
    Nov 17 '18 at 0:54

















I am unsure of what you are asking about, You should make your post short and straight forward

– SamzSakerz
Nov 17 '18 at 0:54





I am unsure of what you are asking about, You should make your post short and straight forward

– SamzSakerz
Nov 17 '18 at 0:54












1 Answer
1






active

oldest

votes


















3














That isn't how catch works. If that shouldn't be printed when there is an exception, you must move it into the body of the try. Like,



// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}


Your method getArea doesn't actually throw the Exception. It prints and swallows it. For the above catch to be invoked, you would also have to modify getArea like



@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
throw e; // <-- add this.
}
}





share|improve this answer


























  • It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

    – Thesar
    Nov 17 '18 at 1:00






  • 1





    @Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

    – Elliott Frisch
    Nov 17 '18 at 1:05











  • Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

    – Thesar
    Nov 17 '18 at 1:11











  • Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

    – Thesar
    Nov 17 '18 at 1:14












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347192%2fjava-exceptions-after-catch-it-stills-prints-whats-after%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














That isn't how catch works. If that shouldn't be printed when there is an exception, you must move it into the body of the try. Like,



// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}


Your method getArea doesn't actually throw the Exception. It prints and swallows it. For the above catch to be invoked, you would also have to modify getArea like



@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
throw e; // <-- add this.
}
}





share|improve this answer


























  • It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

    – Thesar
    Nov 17 '18 at 1:00






  • 1





    @Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

    – Elliott Frisch
    Nov 17 '18 at 1:05











  • Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

    – Thesar
    Nov 17 '18 at 1:11











  • Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

    – Thesar
    Nov 17 '18 at 1:14
















3














That isn't how catch works. If that shouldn't be printed when there is an exception, you must move it into the body of the try. Like,



// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}


Your method getArea doesn't actually throw the Exception. It prints and swallows it. For the above catch to be invoked, you would also have to modify getArea like



@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
throw e; // <-- add this.
}
}





share|improve this answer


























  • It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

    – Thesar
    Nov 17 '18 at 1:00






  • 1





    @Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

    – Elliott Frisch
    Nov 17 '18 at 1:05











  • Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

    – Thesar
    Nov 17 '18 at 1:11











  • Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

    – Thesar
    Nov 17 '18 at 1:14














3












3








3







That isn't how catch works. If that shouldn't be printed when there is an exception, you must move it into the body of the try. Like,



// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}


Your method getArea doesn't actually throw the Exception. It prints and swallows it. For the above catch to be invoked, you would also have to modify getArea like



@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
throw e; // <-- add this.
}
}





share|improve this answer















That isn't how catch works. If that shouldn't be printed when there is an exception, you must move it into the body of the try. Like,



// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}


Your method getArea doesn't actually throw the Exception. It prints and swallows it. For the above catch to be invoked, you would also have to modify getArea like



@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!nn");
throw e; // <-- add this.
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 1:05

























answered Nov 17 '18 at 0:54









Elliott FrischElliott Frisch

157k1397192




157k1397192













  • It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

    – Thesar
    Nov 17 '18 at 1:00






  • 1





    @Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

    – Elliott Frisch
    Nov 17 '18 at 1:05











  • Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

    – Thesar
    Nov 17 '18 at 1:11











  • Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

    – Thesar
    Nov 17 '18 at 1:14



















  • It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

    – Thesar
    Nov 17 '18 at 1:00






  • 1





    @Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

    – Elliott Frisch
    Nov 17 '18 at 1:05











  • Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

    – Thesar
    Nov 17 '18 at 1:11











  • Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

    – Thesar
    Nov 17 '18 at 1:14

















It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

– Thesar
Nov 17 '18 at 1:00





It still does, have tried that also. Can it be something wrong in my getArea()? Should the "return" also be in the try? And if so, how? Because the method will still require a return outside of the try bracets.

– Thesar
Nov 17 '18 at 1:00




1




1





@Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

– Elliott Frisch
Nov 17 '18 at 1:05





@Thesar Yes. Your getArea doesn't ever throw an exception. It silently swallows them instead.

– Elliott Frisch
Nov 17 '18 at 1:05













Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

– Thesar
Nov 17 '18 at 1:11





Thanks, it works now, except for that both throws are geting printed. The print of getArea() "The area cannot..." and the print of getHeight() "The height cannot..."

– Thesar
Nov 17 '18 at 1:11













Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

– Thesar
Nov 17 '18 at 1:14





Solved it by only having throw new ExceptionCheck("The area cannot...") in the catch body in getArea(). Thanks Elliott!

– Thesar
Nov 17 '18 at 1:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347192%2fjava-exceptions-after-catch-it-stills-prints-whats-after%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine