wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes exceptional messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "exceptional messages " Watch "exceptional messages " New topic
Author

exceptional messages

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hello all. Where does e.getMessage( ) get its messages from? Is it when a new exception is created with
new Exception( "here's your message." ) ;
?
Thanks,
Pauline
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
hi, pauline. yes, it gets it's message from a "throws" declaration somewhere below the "try/catch" block. if you were to create your own try/catch block that calls a function which throws exception, then you can make your own "throws message" by using this code:
Exception someName = new Exception( "Your message here." );
then, when something goes wrong (or something does not go right) you can throw that exception by using this:
throw someName ;
that throw message will bubble up through the method calls until it gets back to the try/catch block, which will then print the message...


what?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Exception e = new Exception( String ) -- constructor

catch ( Exception e ){}
You just created an instance of the Exception and named it 'e'.
getMessage() is a method of Throwable, the superclass of Exception. So you are calling the method on the instantiated Exception object, e, which prints the String you put in the constructor.

Clear as mud?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Finally a chance to play in the mud again, a favorite pasttime...

Exception e = new Exception( String ) -- constructor
catch ( Exception e ){}
You just created an instance of the Exception and named it 'e'.

so far so good

getMessage() is a method of Throwable, the superclass of Exception. So you are calling the method on the instantiated Exception object, e, which prints the String you put in the constructor.

It took a few times through, but I think I'm still following.
Now, where does the call to getMessage( ) come in this example, and how does it relate to the catch ( Exception e ) {} ?
Does the exception have to get thrown in there somewhere? (no mud throwing, please)

Pauline
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6

yes, it gets it's message from a "throws" declaration somewhere below the "try/catch" block.

The order of what happens when is still a bit confusing to me. The bubble analogy is good - bubbles always float up.
In your example Greg, the getMessage() isn't called explicitly. Is this because if I use
throw someName ;
the message just gets printed out?
Thanks,
Pauline
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10


[This message has been edited by Marilyn deQueiroz (edited July 31, 2001).]
[This message has been edited by Marilyn deQueiroz (edited July 31, 2001).]
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
sorry, pauline, i was not familiar with the "e.getMessage()" method before today.
if you have a main() method with a try/catch block in it... and within the try/catch block you call a method that "throws" an exception, that exception is what will be returned to the main() method instead of the int, string, or whatever was supposed to be returned.
normally, the default error message will be printed. but, if you make your own message, that will be printed when you "catch" the exception. if you just use "System.out.println( e );" then it will print the type of error and the message that you created.
java.lang.IOException:... your message // or something like that
but, if you use the "e.getMessage()" way, then it will only print your message. so, you have to print the message in your catch block. if you do not print your message there, the default message will be printed after the exception has traveled all the way up the method chain.
[This message has been edited by Greg Harris (edited July 31, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Marilyn,
I put your code inside a class to play with it, but it won't compile. The compiler doesn't like that there are two e's in the same scope:
C:\Michael\javaranch\projects\stuff\Except.java:19: e is already defined in test(java.lang.String)
catch ( Exception e )
^
1 error
Tool completed with exit code 1

I don't understand why you're instantiating an Exception at the top of the method. All the exceptions that you have catch blocks for here are being thrown by the JVM, right?
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
here is something i came up with that might help the try/catch block make sense... i know it is not that great, but it works. when you run it, pass a "1" to see the correct output, or enter any other integer to see the throws statment.

Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Meanwhile, I was writing up this:

Comment out either line (1) or (2), depending on whether you want this to run normally or throw an exception. Comment out either line (3) or (4), depeding on whether you want your exception to have a message.
What I'm trying to show at (A), (B), and (C) is that there are a couple of ways of getting at the information in an exception.
public String getMessage() -- returns the exception's message (if there is one, null if there isn't).
public String toString() -- returns the class name of the exception and the message (if there is one).
public void printStackTrace() -- prints out what you get by calling toString(), then a trace of the method calling stack.
[This message has been edited by Michael Matola (edited July 31, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Just to point this out --
System.out.println( e ) ;
System.out.println( e.toString() ) ;
are equivalent.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Michael Matola:
Marilyn,

I put your code inside a class to play with it, but it won't compile. The compiler doesn't like that there are two e's in the same scope:

C:\Michael\javaranch\projects\stuff\Except.java:19: e is already defined in test(java.lang.String)
catch ( Exception e )
^
1 error

Tool completed with exit code 1

I don't understand why you're instantiating an Exception at the top of the method. All the exceptions that you have catch blocks for here are being thrown by the JVM, right?

You're right, Michael. I was trying not to give everything away, and in the process put
Exception e = new Exception( "any other Exception " );
erroneously within the same method where I tried to create another Exception named 'e' in the catch block.

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Thanks so much everyone. Was away for a couple days, now it's time to play with some code. Sometimes I forget to do that *before* posting questions...

Pauline
[This message has been edited by Pauline McNamara (edited August 04, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptional messages
 
Similar Threads
Error in Accessing
Exception : Server side stack traces
Concept on coding for Exception Handling
Seam: Injection doesn't work in custom validator
How to stop logging to console