Jumping to a catch statement?

hi, if I have the following code:
     private String getDefaultReportName(){
          String reportFolder = "Data\\Report_Name";
          FileInputStream FIS;
          //File reportNameFile;
          try {
               File folder = new File(reportFolder); //check to see if the folder exists first!
               if(folder.exists()){
                              //then do some stuff
                         }//end if
                         else{
                             //if this happens, I want to jump to the catch clause automatically...
                          }//end else
          }//end try
          catch (Exception e) {
               return "Report";
     }I found that, in my code, i have used the 'return "Report";' line often and I would like to
just jump straight to the catch statement if that is possible. I am sure it is, but I don't
know how off the top of my head. How can I jump straight to a catch clause from a try
block? thanks!

I don't know what that's supposed to be doing, or
what point you're trying to make, but it looks like
the wrong way to use/handle exceptions.A coworker at a previous job called exceptions (we were writing in Ada) "glorified GOTO" (complaining about how another coworker used it too liberally). I believe that would apply in this case.

Similar Messages

  • "catch is unreachable" compiler error with java try/catch statement

    I'm receiving a compiler error, "catch is unreachable", with the following code. I'm calling a method, SendMail(), which can throw two possible exceptions. I thought that the catch statements executed in order, and the first one that is caught will execute? Is their a change with J2SE 1.5 compiler? I don't want to use a generic Exception, because I want to handle the specific exceptions. Any suggestions how to fix? Thanks
    try {
    SendMail(....);
    } catch (MessagingException e1) {
    logger.fine(e1.toString());
    } catch (AddressException e2) {
    logger.fine(e2.toString());
    public String SendMail(....) throws AddressException,
    MessagingException {....                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    I found the problem:
    "A catch block handles exceptions that match its exception type (parameter) and exception types that are subclasses of its exception type (parameter). Only the first catch block that handles a particular exception type will execute, so the most specific exception type should come first. You may get a catch is unreachable syntax error if your catch blocks do not follow this order."
    If I switch the order of the catch exceptions the compiler error goes away.
    thanks

  • Biginner's question about catch statement

    hi im just a biginner
    normally u guys write catch statement like
    catch (FileNotFouldException exception)
    System.out.println (exception);
    something like that . but what is the exception here
    is it object name? or class name or what
    i mean the word exception after FileNotFoundException
    i just don't understand

    It's the type of the exception to which the catch block should apply.
    You could have this:
    try {
      // stuff
    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace();
    } catch (IOException ioe) {
      ioe..printStackTrace();
    } catch (NumberFormatException nfe) {
      nfe.printStackTrace();
    } catch (Exception x) {
      ex..printStackTrace();
    }Note that FNFE extends IOE, so if the IOE is caught first, the FNFE would be caught in that block too. That's why you catch the general Exception last - if you do it at all.

  • Control flow: catch statement

    Hi there!,
    Need to know where the control passes if an error is caught in a catch statement.
    Suppose no alternative error handling code has been specified. Where would control go if a terminal error occured? Would it just carry on to the next statement in the general flow or would it terminate the application/applet. By terminal, i mean something like getParameter() returning null etc..
    Any help that sheds light on this is appreciated greatly.
    To make this clearer, how would the program know when to exit a try catch statement if it falls into a catch statement that gave a nonsensical value?

    You should be careful with your terms, "an error is
    caught" has a very specific meaning in Java. Errors
    (instances of java.lang.Error and it's subclasses)
    should not be caught (that is, with a try-catch
    statement) in your code because they signal more
    serious problems than usual (like ClassNotFoundError,
    LinkageError, VirtualMachineError, and so on), not in
    switch statements or otherwise.In certain situations they should be caught.
    If I create a stand alone app with a client GUI it doesn't matter to the user if a null pointer exception, because of programmer error, occurs in the database layer. But they are going to get real upset if the app just abruptly exits which is what is going to happen if the GUI layer doesn't catch all of the exceptions. And although it is possible that one of those exceptions makes it impossible to do anything at all, it is much more likely that the GUI can at least display a message to the user before exiting. And write the stack trace to a file as well.
    This is true at the interface between any two layers. And some applications will be able to continue. I certainly don't want a J2EE server exiting because one of the new beans isn't working correctly. It won't stop the other beans from working.

  • Catch Statement, Help!!!

    I need help with a catch statement that will give an error message when Letters are put in, i need this as im only working with numebers, Heres what i have done already. When i build the code i always get
    ";" expected
    Thanks for your Help
    jMenuKmperHourMileActionPerformed(java.awt.event.ActionEvent e)
    try
    String Number = ToBeConverted.getText();
    double amount = new Double(Number).doubleValue();
    total = (amount*.621371192);
    Converted.setText("= " + total);
    ToBeConverted.setEditable(false);
    catch(NumberFormatException nfx)
    Converted.setText(Number + " is not a valid number!");
    }

    Your try/catch statement is bad. You have too many { } brackets in there.
    The format should be
    try{
      // code which throws Exceptions
      String Number = ToBeConverted.getText();
      double amount = new Double(Number).doubleValue();
      total = (amount*.621371192);
      Converted.setText("= " + total);
      ToBeConverted.setEditable(false);
    catch(NumberFormatException nfx){
      // error handler
      Converted.setText(Number + " is not a valid number!");
    }

  • Infinite loop in a catch {} statement, should be simple

    Hi, I'm trying to take integer input from the console using a Java Scanner object, and I enter an infinite loop if there is an IO error inside the catch statement. Please let me know if you know what I'm doing wrong:
    do
    try
    Problem = false; //this is a bool
    start = Input.nextInt(); // Input is a Scanner, start is an Intenger
    catch (Exception IOException) {
         Problem = true;
         System.out.println("Bad value.\n");
         //The infinite loop is inside this statement.
    while (Problem == true);
    This code block is intended to take integer input, then, if there's an IO error, try to do it again, indefinitely, until it has successfully given Start a value. As it stands, if executed, if there is not an IO error, it works; if there is an IO error it enters an infinite loop in the catch statement. Please forgive my incompetence, any help is appreciated :)

    Hi, thanks for the advice to both of you, your suggestion that it is stuck seems to be correct.
    I add to the catch statement:
    Input = new Scanner(System.in);
    To reset it, and it works now. This is probably not the best way to do things, but I'm just a student and it's for a homework assignment that does not require try / catch / for so it works for this, thank you for the help! :)

  • Exception Handling after the Catch Statement

    Hi every one,
    How to handle the exceptions after Catch Statement.
    What does a catch statement can do?
    Regards,
    Johny

    Hi,
    Check this link:
    http://help.sap.com/saphelp_nw70/helpdata/en/a9/b8eef8fe9411d4b2ee0050dadfb92b/content.htm
    http://help.sap.com/saphelp_nw70/helpdata/en/cf/f2bbce142c11d3b93a0000e8353423/content.htm
    Regards
    Adil

  • Regards catch statement

    Is there any thing worng in my catch statements .because when i do debug the curser not catching the catch statements
    TRY.
           CALL METHOD z_co_ifoasy_01_sa_gl=>execute_asynchronous
              EXPORTING
                output = gv_zsafe_mt_acc_gl_posting01.
          CATCH cx_ai_system_fault INTO s_exception.
          CATCH cx_ai_application_fault INTO s_exception_app.
        ENDTRY.
       IF ( s_exception IS  INITIAL AND s_exception_app IS INITIAL )
          MESSAGE i007.
        ELSE.
          PERFORM update_log_table.
          COMMIT WORK.
          MESSAGE i009. "Message sent to XI".
        ENDIF.
      ENDIF.

    hi,
        What type of Exceptions you want to catch in Proxy?
         Try to do commit work before you read exception variables for text and code.
    Follow this process:
    Proxy call
    catch exception
    commit work.
    Use Exception variale to read details.
    If you want to check that RFC Destination that pings your PI server is fine or not.
    Call FM: BDL_SERVER_PING
    to check if your PI server is down.
    Regards,
    Anurag Garg

  • Which catch statement, returns null

    just wanted to if there is any catch statement which returns if the file is null
    something like
    catch(NullPointException ex){
    }

    Ashish.Uppin wrote:
    just wanted to if there is any catch statement which returns if the file is null
    something like
    catch(NullPointException ex){
    Can I catch a null pointer exception? Yes.
    Do I? No
    Can you? Probably not.

  • Regards Catch statements

    Is there any thing worng in my catch statements .because when i do debug the curser not catching the catch statements
    TRY.
    CALL METHOD z_co_ifoasy_01_sa_gl=>execute_asynchronous
    EXPORTING
    output = gv_zsafe_mt_acc_gl_posting01.
    CATCH cx_ai_system_fault INTO s_exception.
    CATCH cx_ai_application_fault INTO s_exception_app.
    ENDTRY.
    IF ( s_exception IS INITIAL AND s_exception_app IS INITIAL )
    MESSAGE i007.
    ELSE.
    PERFORM update_log_table.
    COMMIT WORK.
    MESSAGE i009. "Message sent to XI".
    ENDIF.
    ENDIF.

    Try like below...
    TRY.
    CALL METHOD z_co_ifoasy_01_sa_gl=>execute_asynchronous
    EXPORTING
    output = gv_zsafe_mt_acc_gl_posting01.
    CATCH cx_ai_system_fault INTO s_exception.
    lv_exception = 'X'.
    CATCH cx_ai_application_fault INTO s_exception_app.
    IF ( lv_exception IS INITIAL AND s_exception_app IS INITIAL )
    MESSAGE i007.
    ELSE.
    PERFORM update_log_table.
    COMMIT WORK.
    MESSAGE i009. "Message sent to XI".
    ENDIF.
    ENDTRY.
    Edited by: Veeranji Reddy on Aug 7, 2009 5:15 PM

  • Try catch statement / unable to reference variables.

    Hi
    How do I get around this:
        public static int countLines()
         try {
             FileReader inputFile = new FileReader("InputFile.txt");
             catch(IOException e){out.println("There is a problem with the InputFile");}
             LineNumberReader in = new LineNumberReader(inputFile);
             Boolean line = null;
             int lineCount = 0;
             while (line = in.readLine() != null)
             lineCount = in.getLineNumber();
            return lineCount;        
        }I get the good old: "cannot find symbol" error for inputFile.
    I have tried putting the t-c statement around the whole thing but then that messes up the return statement. Then ofcourse if I put the return statement outside of the t-c statement I have the same "cannot find symbol" error with the lineCount variable.
    I just want to flag up a problem if the input file has the wrong name or isn't where it should be.
    Thanks in advance
    Dan

    However when I redeclared all the variables with a
    bigger scope (as per your code, outside of the try
    statement) it failed with incompatible types on the
    line shown above.
    line is a string but in.readLine is boolean.
    in.readLine() returns a String
    you had a Boolean
    since you're not doing anything with the line, then you can use this
    (without the String line)
        public static int countLines() {
            int lineCount = 0;
            FileReader inputFile=null;
            LineNumberReader in = null;
            try {
                inputFile = new FileReader("InputFile.txt");
                in = new LineNumberReader(inputFile);
                while ( in.readLine() != null ) {
                    lineCount = in.getLineNumber();
            catch(IOException e){
                System.out.println("There is a problem with the InputFile");
            finally{
                try{
                    if(inputFile !=null)
                        inputFile.close();
                    if(in != null)
                        in.close();
                }catch(IOException ioex){
                    System.out.println("can't close this!");
            return lineCount;
      

  • Fault MT - CATCH statement

    Hi,
    I have a sync proxy to proxy scenario.
    I am handling an exception in my client proxy ...
    CATCH z_cx_test_fault INTO lo_cx_test_fault.
    txt = lo_cx_test_fault->get_text( ).
    WRITE txt
    I am sending back some fault text and fault detail message in the fault MT from the receiver proxy
    however the txt variable that is printed here only shows a text called " application error" instead of the the text that i have sent inside the fault MT from the receiver proxy..

    Dear Hema
    Are you using a standard Fault MT or is it a customized one?
    Generally the text "Application Error" is populated in following way in standard fault message
      <SAP:Category>Application</SAP:Category>
      <SAP:Code area="UNKNOWN">APPLICATION_ERROR</SAP:Code>
    I think you should create a MT of Fault Message Type and make necessary changes in the proxy to handle that
    Sourabh

  • How to shorten many catch() statements?

    In the code like the following..
    try {
        // the code which generates many kind of exceptions
    catch(FirstException x) {
        // follows a lot of exceptions
    catch(SecondException x) {}
    catch(ThirdException x) {}I want to shorten it without crashing the explicitness. Then, how??

    I'm using JTAPI. There is a exception heaven. For example,
    public Connection[] connect(Terminal origterm,
                                Address origaddr,
                                java.lang.String dialedDigits)
                         throws ResourceUnavailableException,
                                PrivilegeViolationException,
                                InvalidPartyException,
                                InvalidArgumentException,
                                InvalidStateException,
                                MethodNotSupportedExceptionIt's a war against exceptions. -_-;;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Return statement and Try Catch problem

    Hi!!
    I've got the next code:
    public ResultSet DBSelectTeam(String query) {
    try {
    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery(query);
    return rs;
    } catch (Exception err) {
    JOptionPane.showMessageDialog(null, "ERROR: " + err);
    But I need a return statement in the catch-block, but I don't know what's the best option.
    Help...
    Many thanks.

    The error message is: "missing return statement", Yes, I know.
    You have to either return from the catch statement, or throw from the catch statement, or return or throw after the catch statement.
    The only ways your method is allowed to complete is by returning a value or throwing an exception. As it stands, if an exception is thrown, you catch it, but then you don't throw anything and you don't return a value.
    So, like I said: What would you return from within or after catch? There's no good value to return. The only remotely reasonable choice would be null, but that sucks because now the caller has to explicitly check for it.
    So we conclude that catch shouldn't return anything. So catch must throw something. But what? You could wrap the SQLE in your own exception, but since the caller is dealing with JDBC constructs anyway (he has to handle the RS and close it and the Statement), there's no point in abstracting JDBC away. Plus he has to deal with SQLE anyway in his use of the RS and Statement. So you might as well just throw SQLE.
    So since you're going to just throw SQLE anyway, just get rid of the try/catch altogether and declare your method throws SQLException

  • Help me with a state machine design

    I have a little bit of a problem.  I have a system with several modes, each selected by the user and called with a case structure.  Each case in the case structure is its own state machine with a few states (like 4-6).  Now here's the catch.  I need one of the states within one of the modes to call and execute an entirely different mode (and all of its states) without actually changing the mode command, and to do so every iteration until I jump out of that state into another state within the first mode.
    Is there a simple way to do that that I'm just not thinking of?
    Lee Jay

    Lee, my first thought would be to use just a big state machine.  For example if each mode needs 4 states, mode 1 gets states 0-3, mode 2 get states 4-7, and so on.  This way, if you need to change modes in the middle of another mode, you just direct the state selector to the state that starts the mode.  (If that makes sense).  It's not real elegant, but it works fairly well as you still maintain the flexibilty of the state machine design, including the ability to add and rearrange cases. 
    Don't forget you can use any method to select the cases, including strings, so you have full flexibility with how you setup case select.
    Troy

Maybe you are looking for

  • ITunes 7 and Nano No-No

    My system is completely in error since the update of iTunes 7. The Nano mounts in the finder and i can start iTunes 7 but when i doubleclick a file on the Nano iTunes hangs, system hangs everything very awkward. Connected to my powerbook (itunes 6.04

  • PI does not keep sequence of XSD for external definition

    Hi, currently I face following problem. I have an XSD which decribes a MSCONS market message that contains following sequence:      <xs:complexType name="MSCONS">           <xs:sequence>                <xs:element name="UNH" type="UNH"/>             

  • While in PDF format it is showing some starting fields only

    Hi All, I created sample report and build Layout template in word doc. I uploded RTF template into Report & given output format in Exel&Pdf. When i view the output for Exel format is OK While in PDF format it is showing some starting fields only (I c

  • Save to web

    I have some strange color issues with PS when I save to web. In Photoshop, the image will look fine but as soon as I save to web, no matter what color format I save to, it looks bleached out- on the blue side. I've tried saving TO different file form

  • Spry:if image file exists?

    Hi, is there a easy way to test if an image file exists on the server? I try to generate dynamic html listing names and pictures. The names (and other info) arein a xml file. For some, images exist on the server as name.jpg file. I'd like to check wi