LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2020, 03:00 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,800

Rep: Reputation: 140Reputation: 140
what is java catch exception code for I/O and exec functions?


I have:
Code:
    try {
        fs = new FileOutputStream(cmdFile);
        ps = new PrintStream(fs);
    }
    catch (Exception e) { return e.getErrorCode(); }

// and
    Process p = null;

    try {
        p = Runtime.getRuntime().exec("chmod +rx " + cmdFile);
        p.waitFor();
    } catch ( Exception e) { return e.getErrorCode(); }
but I get:
Code:
An error occurred at line: [19] in the jsp file: 
getErrorCode cannot be resolved or is not a field
This is part of a function and I want to return the error status code of the failure. How can I do that?

Last edited by mfoley; 11-20-2020 at 03:04 PM.
 
Old 11-20-2020, 03:04 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,494
Blog Entries: 11

Rep: Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466Reputation: 3466
Shouldn't getErrorCode be a function call: getErrorCode() ?
 
Old 11-20-2020, 03:33 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,104

Rep: Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499
Java Exception doesn't have such method; there's a getMessage method that returns a text-message, which might contain an error-code.
 
2 members found this post helpful.
Old 11-20-2020, 05:56 PM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 368

Rep: Reputation: Disabled
As seen in NevemTeve's post javadocs are a good source of information on Java's classes. If you look up FileOutputStream and PrintStream you'll each has specific exceptions as well so you can narrow the catch to specific exceptions.
i.e.
Code:
 try {
   fs = new FileOutputStream(cmdFile);
   ps = new PrintStream(fs);
} catch (FileNotFoundException fne) { 
   System.err.println (fne.getMessage ()); 
} catch (Exception e) {
  System.err.println (e.getMessage ());
}
If you want to return something you can either return a string or a Throwable object.

Last edited by SoftSprocket; 11-20-2020 at 06:01 PM. Reason: Posted before complete
 
1 members found this post helpful.
Old 11-21-2020, 01:04 AM   #5
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,800

Original Poster
Rep: Reputation: 140Reputation: 140
OK, thanks. My function returns an int, so getMessage() won't help, and I don't want to parse the returned text. Oh well. What I've done in the program is return a distinctive error code:
Code:
    try {
        fs = new FileOutputStream(cmdFile);
        ps = new PrintStream(fs);
    }
    catch (Exception e) { return -1000; }

    try {
        p = Runtime.getRuntime().exec("chmod +rx " + cmdFile);
        p.waitFor();
    } catch ( Exception e) { return -2000; }
That will at least tell me where in the function the failure occured and I can debug in a targeted way.

I was hoping that an error code was always returned by an exception and that I was just missing it, but I guess not.

astrogeek - yes, getErrorCode() does need the "()". I edited my posting, but apparently not before you replied. Nor did I think to change the error.
 
Old 11-21-2020, 03:06 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,104

Rep: Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499Reputation: 1499
Note: file opening methods return FileNotFoundException for any problem, only the message contains the actual reason, eg:

Code:
java.io.FileNotFoundException: /test1 (Permission denied)
java.io.FileNotFoundException: test2 (Is a directory)
 
Old 11-21-2020, 11:17 AM   #7
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 368

Rep: Reputation: Disabled
Quote:
Originally Posted by mfoley View Post
OK, thanks. My function returns an int, so getMessage() won't help, and I don't want to parse the returned text. Oh well. What I've done in the program is return a distinctive error code:
Code:
    try {
        fs = new FileOutputStream(cmdFile);
        ps = new PrintStream(fs);
    }
    catch (Exception e) { return -1000; }

    try {
        p = Runtime.getRuntime().exec("chmod +rx " + cmdFile);
        p.waitFor();
    } catch ( Exception e) { return -2000; }
That will at least tell me where in the function the failure occured and I can debug in a targeted way.

I was hoping that an error code was always returned by an exception and that I was just missing it, but I guess not.

astrogeek - yes, getErrorCode() does need the "()". I edited my posting, but apparently not before you replied. Nor did I think to change the error.
The "java way" is to throw the exception to the calling level you want to handle it at - in other words you add throws clauses to the methods that can throw but aren't at a level where you wish to handle the exception. If there is inefficiency in unwinding the stack it's too bad since java enforces exception handling - efficiency in error handling becomes, primarily, the virtual machines problem.

The calling stack itself is an object so you can extract information from it - for what it is worth.
i.e.
Code:
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream ("./missing_file.txt");            
        } catch (FileNotFoundException e) {
            System.out.println (e.getMessage ());
            StackTraceElement topElement = e.getStackTrace()[0];

            System.out.println(topElement.getClassName());
            System.out.println(topElement.getMethodName());
            System.out.println(topElement.getLineNumber());
            System.out.println(topElement.getModuleName());
            System.out.println(topElement.isNativeMethod());
        } finally {
            if (inputStream != null) { inputStream.close(); }
        }
giving us something like:
Code:
./missing_file.txt (The system cannot find the file specified)
java.io.FileInputStream
open0
-2
java.base
true
I presume since it is in native code we don't get the line number where error actually occurred.
 
  


Reply

Tags
exception, java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: We love Kubernetes, but it's playing catch-up with our Service Fabric, says Microsoft Azure exec LXer Syndicated Linux News 0 11-06-2018 08:00 AM
Input in integers, users writes a char, how to catch exception? raskol Programming 4 03-30-2008 01:28 PM
I Need a program do try-catch for Exception In Linux? saeedeh1363 Programming 2 09-04-2007 06:49 AM
Java: try won't catch exception nro Programming 3 09-16-2004 12:28 AM
Runtime Exception vs. Exception mikeshn Programming 1 09-22-2002 06:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration