try - catch box

C

cCss..........

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.
 
D

deepak.vaswani

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.


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!");
}
}
 
A

Andrew Thompson

cCss.......... said:
Consider the following code:

try
{
a=func(); // line-1
- - - -- - - - - -
- - - - - - -- - - -
}
catch(Exception e)
{
- - - - -

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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200801/1
 
A

Andrew Thompson

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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200801/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top