Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

try - catch box

Thread view: 
cCss.......... - 04 Jan 2008 07:15 GMT
Consider the following code:

try
{
  a=func();            // line-1
  - - - -- - - - - -
  - - - - - - -- - - -
}
catch(Exception e)
{
- - - - -
}
System.out.println(a);  //  line-n

In the code func() returns any value to 'a'. If any exception occurs
after line-1. Would line-n print the value of a, or the program will
terminated after catching the exception.
deepak.vaswani@gmail.com - 04 Jan 2008 09:05 GMT
> Consider the following code:
>
[quoted text clipped - 13 lines]
> after line-1. Would line-n print the value of a, or the program will
> terminated after catching the exception.

Hi ,

try / catch block is to handle the exception carefully . so if any
exception occurs in try block then it is catch block duty to handle it
properly . and after that everything thing will be perfomed
successfully . provided variable a should be defined and declared
earlier outside the try block properly .

class test
{
    public void test1()
    {
        int s = 0 ;
        try
        {
            s=test2();
            int x=0;
            int z=9;
            int y =z/x;
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        System.out.println(s);
    }

    public int test2()
    {
        int f=10;
        return f;
    }
    public static void main(String[] args)
    {
        test t = new test();
        t.test1();
        System.out.println("Hello World!");
    }
}
Andrew Thompson - 04 Jan 2008 09:22 GMT
>> Consider the following code:

I prefer deepak's code, since it is an SSCCE (in
all but 'code delimiters').

>try / catch block is to handle the exception carefully .
(snip)

Just one suggestion (OK, 2)..

>        catch (Exception e)
>        {
>            System.out.println(e);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Syste... *

It is generally more informative (less typing too!) to simply..
 e.printStackTrace();

Please replace tabs with 2-4 (I prefer 2) spaces before
posting code examples to usenet.  Most news clients
expand tabs to ridiculous widths.

I made a simple little tool to both check line width, and
replace tabs.  You can find a link to the TWC launch file
here..
<http://www.physci.org/codes/sscce.html#linewidth>

* The line of hats '^' shows how far that last line indents in
my 'news client'.  I expect it is similar for a lot of people.

Signature

Andrew Thompson
http://www.physci.org/

Andrew Thompson - 04 Jan 2008 09:12 GMT
>Consider the following code:
>
[quoted text clipped - 7 lines]
>{
>- - - - -

What is here?

>}
>System.out.println(a);  //  line-n
>
>In the code func() returns any value to 'a'. If any exception occurs
>after line-1. Would line-n print the value of a,

Yes, given as written, 'a' must have been declared outside
the try construct, 'a' will contain the result of func() after the
try/catch completes.

>..or the program will
>terminated after catching the exception.

A lot of this depends on "What is here?"

If it is something like..
 e.printStackTrace();
..then the program will dump the stacktrace, and continue
to print the value of 'a'.  OTOH, if that line is..
 System.exit(int);
..then, no.  The app. will terminate before printing the
result stored in/referenced by 'a'.

Note the 'caveats/ifs and buts' in the above explanation.
I would not have had to cover as many possibilities if your
code had been complete, as might be seen in an SSCCE*.

Please consider posting SSCCEs when asking questions
of this nature.  In fact, now I think about it, an SSCCE would
have answered the initial question for you.  The only remaining
questions might have been "Why does the example behave this
way?" or "How do I change the behaviour to..?".

* <http://www.physci.org/codes/sscce.html>

Signature

Andrew Thompson
http://www.physci.org/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.