Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Siva,
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Thank You.
-siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic