• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transfer exception from one method to main

 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I learn for OCAJP 7.

How do I write the code so transfer my exception from the reverse method:



I don't want to put try/catch in main.

Can you help me?

Thank!!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anda Cristea wrote:
I don't want to put try/catch in main.



If you want the error message in the exception, but don't want to catch the exception, then your method simply can't throw the exception.

Possible alternatives are ... Modify the return type to return a combination of the exception or result. Have some sort of instance/static variable setup, which contains the exception.  Basically, the exception should be treated like any other piece of data.

Henry
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anda,
if you don't want to catch the exception in main you can make it throw again,simply add the throws clause followed by exception in the signature of main.
Anyway you can better indent your code.look at line 15 their should be a space before braces,look at line 12 and then look at line 21,26,33-you will find the difference-they are 2 different styles of indentation.don't mix it up as it makes your code less comprehensible.
Kind Regards,
Praveen.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anda Cristea wrote:How do I write the code so transfer my exception from the reverse method:

I don't want to put try/catch in main.


First of all, you should explain in great detail what you want to achieve if you want to get great advice/help.

Because I am unsure about what you are trying to achieve, I made a guess. The following code snippet uses a runtime exception (instead of a checked one) and that removes the requirement of the try-catch statement. I kept the try-finally statement to print the Program completed message. If that's also not required, you can remove the try-finally statement tooIf you are trying to do something else, please provide a detailed explanation of what you are looking for.

Hope it helps!
Kind regards,
Roel
 
Dana Ucaed
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Because I am unsure about what you are trying to achieve, I made a guess. The following code snippet uses a runtime exception (instead of a checked one) and that removes the requirement of the try-catch statement. I kept the try-finally statement to print the Program completed message. If that's also not required, you can remove the try-finally statement too



Thanks!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic