• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exceptional messages

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[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
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to point this out --
System.out.println( e ) ;
System.out.println( e.toString() ) ;
are equivalent.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic