Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception" Watch "exception" New topic
Author

exception

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
I have placed pretty the same two code.
What is the difference. If I un-comment "// throw new SimpleExceptio", I
have problem in compiling the code. Please can you explain, why do I have compilation problem ?

thanks
siva



[CODE:1] class SimpleException extends Exception {}

public class SimpleExceptionDemo {

public void f() throws SimpleException {
System.out.println( "Throwing SimpleException from f()");
throw new SimpleException ();
}


public static void main(String[] args) {
SimpleExceptionDemo sed = new SimpleExceptionDemo();


try {


// throw new SimpleException ();

if (sed != null ) {
throw new SimpleException (); }

System.err.println("Caught it-3!");

} catch(SimpleException e) {
System.err.println("Caught it-main()!");
}



}
} ///:~
[/CODE]




[CODE:2] class SimpleException extends Exception {}

public class SimpleExceptionDemo {

public void f() throws SimpleException {
System.out.println( "Throwing SimpleException from f()");
throw new SimpleException ();
}


public static void main(String[] args) {
SimpleExceptionDemo sed = new SimpleExceptionDemo();


try {


throw new SimpleException ();

if (sed != null ) {
throw new SimpleException (); }

System.err.println("Caught it-3!");

} catch(SimpleException e) {
System.err.println("Caught it-main()!");
}



}
} ///:~
[/CODE]
[ August 01, 2004: Message edited by: Siva kandasamy ]
Ryan Smith
Ranch Hand

Joined: Jun 29, 2004
Posts: 40
There is fundamentally no difference if you un-comment //throw new SimpleException(); All you do is force it through the if statement, upon which it will throw the same exact exception.

I just compiled your code fine. If you are having problems, check the file name - be sure that it is saved under SimpleExceptionDemo.java, which is the main class.
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi
Did you compile the code:2. ie. code I placed below in my posting.
Here is the error message:

~/rd/java/error % javac "SimpleExceptionDemo.java"
SimpleExceptionDemo.java:20: unreachable statement
if (sed != null ) {
^
1 error
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi Siva,
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Perfect. Thank You.
-siva
 
 
subject: exception
 
Similar Threads
Finally not run
if finally gives an exception ?
Constructor Exception
exception
Strange Exception Behaviour