public class exception4{


public static void method(){

try{
exception4.method2();
throw new Exception("method throw");
}
catch(Exception e)
{
System.out.println("method Exception caught"+ e);
//throw e;
}


}

public static void method2(){

try{
throw new Exception("method2 throw");
}
catch(Exception e)
{
System.out.println("method2 Exception caught"+ e);

e.printStackTrace();
[B]//throw e;  
[/B] [U]if include this statement it show error [/U]
}

}

public static void main(String args[]){

System.out.println("exception program");

try{

exception4.method();

}
catch(Exception e)
{
System.out.println("main Exception caught"+ e);
}

}


}

same problem in the case of user defined exception also..

class MException extends Exception{

public MException(String str)
{

super(str);


} 



} 







public class exception4{


public static void method(){

try{
exception4.method2();
throw new MException("method throw");
}
catch(MException e)
{
System.out.println("method Exception caught"+ e);
throw e;
}


}

public static void method2(){

try{
throw new MException("method2 throw");
}
catch(MException e)
{
System.out.println("method2 Exception caught"+ e);

e.printStackTrace();
//throw e;

}

}

public static void main(String args[]){

System.out.println("exception program");

try{

exception4.method();

}
/*catch(MException e)
{
System.out.println("main Exception caught"+ e);
}*/

finally{
}

}


}

exception4.java:33: unreported exception MException; must be caught or declared
to be thrown
throw e;
^
1 error

First example:

public class exception4 {

	public static void method() throws Exception { // 4. Added "throws Exception" here.

		try {
			exception4.method2();
			throw new Exception("method throw");
		} catch (Exception e) {
			System.out.println("method Exception caught" + e);
			throw e; // 3. Uncommented this line.
		}
	}

	public static void method2() throws Exception { // 2. Added "throws Exception" here.
		try {
			throw new Exception("method2 throw");
		} catch (Exception e) {
			System.out.println("method2 Exception caught" + e);

			e.printStackTrace();
			throw e; // 1. Uncommented this line.
		}
	}

	public static void main(String args[]) {
		System.out.println("exception program");
		try {
			exception4.method();
		} catch (Exception e) {
			System.out.println("main Exception caught" + e);
		}
	}
}

Second example:

class MException extends Exception {
	private static final long serialVersionUID = 1L;  // 5. to fix this warning: "The serializable class MException does not declare a static final serialVersionUID field of type long"
	public MException(String str) {
		super(str);
	}
}

public class exception4 {

	public static void method() throws MException { // 4.
		try {
			exception4.method2();
			throw new MException("method throw");
		} catch (MException e) {
			System.out.println("method Exception caught" + e);
			throw e; // 3.
		}
	}

	public static void method2() throws MException { // 2.
		try {
			throw new MException("method2 throw");
		} catch (MException e) {
			System.out.println("method2 Exception caught" + e);

			e.printStackTrace();
			throw e; // 1.
		}
	}

	public static void main(String args[]) {
		System.out.println("exception program");
		try {
			exception4.method();
		} catch (MException e) {
			System.out.println("main Exception caught" + e);
		} finally {
		}
	}
}

If you find that you don't like to have to deal with so many "try-catch" blocks and "throws" clauses, then consider extending RuntimeException .

(Now that the code works, here is something else to consider: It's generally a bad idea to both "handle" an exception (by logging or printing it, for example) and also rethrow it. "Handle it or throw it. Don't do both." is a good general rule.)

If you find that you don't like to have to deal with so many "try-catch" blocks and "throws" clauses, then consider extending RuntimeException .
(Now that the code works, here is something else to consider: It's generally a bad idea to both "handle" an exception (by logging or printing it, for example) and also rethrow it. "Handle it or throw it. Don't do both." is a good general rule.)

thank u sir i got it...

  1. but without final long serialVersionUID (variable)..its working fine ..what is the purpose of that variable...

  2. can we use fillinstacktrace()method to update exception...or default it will update..

  3. another in exception creation

Code:

class MException extends Exception{


public MException(String str)
{
super(str);
}
} 


super (str)-----> it will send the string to base class constructor(Exception class)--->in the exception class it will send base class constructor(Throwable class)


in lang packge,exception class
public Exception(String message) {
super(message);
}



in lang packge,throwable class
public Throwable(String message) {
fillInStackTrace();//shall we use this method manually..
detailMessage = message;
}

but my question is how to assign the string("exception parameter or error msg") to exception object...

if we print the object it will print the error message....

The Java compiler warning message, "The serializable class MException does not declare a static final serialVersionUID field of type long" is a WARNING message, not a "stop the presses" error. So you can ignore it if you want.

If you fail to provide a serialVersionUID value, the compiler will generate one for you automatically. This is really only a problem if you "save a copy" of the exception message in some persistent store and then run an updated version of your program after changing the implementation of that exception. In that case, you will have problems restoring the saved object to memory.

Since it is highly unlikely that you will attempt to do the above, you can feel free to ignore the warning message.

Some warnings, however, indicate serious problems in programs. They can often point out real bugs. So I make a habit of "fixing" all warnings. Good clean code should compile with no warning messages at all. You have to define the serialVersionUID variable to get rid of that warning message. It doesn't really matter what value you give it. It's a version number, so starting from one does make a fair amount of sense.

Calling super in a constructor does actually call the corresponding superclass constructor and execute its code. So fillInStackTrace(); does get called for you; you don't have to call it again.

Regarding your last question...

but my question is how to assign the string("exception parameter or error msg") to exception object...

if we print the object it will print the error message....

Hmmm... It works fine in both of your example programs above.

Try doing this in method2: throw new MException("exception parameter or error msg"); I get this result:

exception program
method2 Exception caughtMException: exception parameter or error msg
method Exception caughtMException: exception parameter or error msg
main Exception caughtMException: exception parameter or error msg
MException: exception parameter or error msg
	at exception4.method2(exception4.java:22)
	at exception4.method(exception4.java:12)
	at exception4.main(exception4.java:34)

Perhaps you are changing the throw in the first method. That throw never executes because the call to method2 throws an exception, causing the calling method to skip down to the catch block.

Regarding your last question...

Hmmm... It works fine in both of your example programs above.

Try doing this in method2: throw new MException("exception parameter or error msg"); I get this result:

exception program
method2 Exception caughtMException: exception parameter or error msg
method Exception caughtMException: exception parameter or error msg
main Exception caughtMException: exception parameter or error msg
MException: exception parameter or error msg
	at exception4.method2(exception4.java:22)
	at exception4.method(exception4.java:12)
	at exception4.main(exception4.java:34)

Perhaps you are changing the throw in the first method. That throw never executes because the call to method2 throws an exception, causing the calling method to skip down to the catch block.

k sir my question is.. how the exception object assign string parameter(error msg)....how the object print error message..(y am asking again is same thing follows on thread also but in different manner)

in exception - error message or error name

in thread- thread name

in exception creation "class MException extends Exception{

public MException(String str)
{
super(str);[B]------->this cons method how to assign string parameter to object...[/B]
} 
} "



in lang packge,exception class
public Exception(String message) {
super(message);
}


in lang packge,throwable class
public Throwable(String message) {
fillInStackTrace();//shall we use this method manually..
detailMessage = message;//fiinally it string message stopped here
}

2.what is the difference between system.out.println and system.err.println
both are print same...

k sir my question is.. how the exception object assign string parameter(error msg)....how the object print error message..

If your question is "How did the exception print the error message I passed to it when I created it, along with a stack trace?", then the answer is that one of your catch blocks does this: e.printStackTrace();

2.what is the difference between system.out.println and system.err.println
both are print same...

Yes, by default both print to the console.

See: http://download.oracle.com/javase/6/docs/api/java/lang/System.html

Under "Field Summary", you'll see the definitions of "err", "in" and "out".

In Unix, and other POSIX-compatible systems (such as Microsoft Windows) every program that runs is given, by default "standard input", "standard output" and "standard error" input and output files. These map to Java's "System.in", "System.out" and "System.err". By default, standard input / System.in is keyboard input, and both "outputs" display on the screen/console.

Something that can be very confusing is the lines written to System.out and System.err are buffered; they are not printed right away. So they may appear on your screen in a different order than they happened. All the lines written to System.out will be in the correct order. But the relative positions of "out" and "err" lines may be unpredictable.

So...
You often see exception messages and stack traces "interleaved" with "System.out" messages that you printed. In other words, they display on the screen "in the wrong order."

This is normal. It is not a bug in your program or in Java. That's just the way things work.

If your question is "How did the exception print the error message I passed to it when I created it, along with a stack trace?", then the answer is that one of your catch blocks does this: e.printStackTrace();

actually when ever exception throwned it will stored in a stack...
if view all exception throwned in the program we use printstacktrace method...

my question is...

in catch block we print a object ...how it will print a error message...

how assign error message to that object...

actually when ever exception throwned it will stored in a stack...
if view all exception throwned in the program we use printstacktrace method...

Not quite: The system does not store a stack or history of all exceptions thrown in the program. Each exception has a class (some class that extends Throwable or one of its children), a String message, and the "stack trace" array. The "stack trace" array contains a representation of the stack at the time the exception was created. In a sense, it is a very limited history of the methods that called the method that created the exception.

(The initCause/getCause methods can be seen as a stack of exceptions. But it is used only when one exception causes another or if you want to nest exceptions. It is not a history of all exceptions thrown.)

my question is...

in catch block we print a object ...how it will print a error message...

how assign error message to that object...

If you want to get the error message from an exception, call the getMessage() method:

/**
     * Returns the detail message string of this throwable.
     *
     * @return  the detail message string of this <tt>Throwable</tt> instance
     *          (which may be <tt>null</tt>).
     */
    public String getMessage() {
        return detailMessage;
    }

Your program does print the error message ("MException: method2 throw") three times, by using the .toString() method of the exception. If you want to get the String "method2 throw" from the exception, then call the getMessage() method.