• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Exception handling  RSS feed

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above code we explicitly try to throw a exception but how does it work ? // line 6
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik : Welcome to JavaRanch.
How does java library work in his case? Since this is a constructor in NullPointerException class how
can we override it? Or what happens? I am not getting the roadmap to using the library.
}

Can you be more specific about what you don't understand?

Jim ... ...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pratik D mehta............the question is not much clear...........please make it clear....

if you wanna know the output then it would be..................



 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know what throw new nullpointerexception does ?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote:I want to know what throw new nullpointerexception does ?



it will throw a object of nullpointerexception that will catch by first with the nearest catch block....or if that is not present then it will propogate back to the calling method.where it will be catch..........
as this is a runtime exception.........compilation will be fine even if we cannot catch it...............

but at runtime this will stop the code to execute if not handled properly.................


Thanks
Shanky
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratik,

The throw statement does exactly what it says. It will throw the just created NullPointer exception.

And I am not sure what you mean by "overriding the constructor" , that my friend can never be done.

You have access to overloaded version ... new NullPointerException("String");

Other than that, please elaborate your question or break it down into more subquestions so that we can discuss this further.

Thanks for your post.

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup now i understand that it creates an instance of nullpointerexception class but the instance does not have a name ,
It just says throw new nullpointerexception() , It just creates a instance but with what name ?
For eg - we write student s1 = new student();
So the name is s1 for the instance ,
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratik,

I hope you are reading Kathy & Berts book,if not that is the best place to start.

By "name" I am assuming you are saying the "reference identifier" is not created.

You can create a reference in the code itself but if will be of little use since you want throw the object again.




Above example as well as the code in your example is completely legal in Java
i.e. to create a object instance without having a reference identifier assigned to it.

you can also do this :

This is useful when you are not worried about having a reference to the object but maybe the
work it does.

For e.g.


Here you create a Worker instance for the sole purpose of calling the doJob method on it.
You do not care about retaining any reference to it since after the job is done the object is no logner required.
Generally it depends on your style. Some people think readbility is more important & assign a reference before
any operations on the instance.

This type of example will also come up when you go through I/O materials where Streams are just chained in this manner.

It is important to remember that this is an no-reference orphan object which might be garbage collected. (not always)

But since we throw the newly created object in an exception flow we have it available at next catch level.
Until a catch completely handles the exception the object will exist & will be referenced by the catch blocks.

So the catch block in "main" now handles the exception & ends gracefully.

I also think that the class name is a misnomer. This is not a example of nested try this plain old exception flow.
A nested try will have a try-inside-a-try


Please let us know if we were able to solve your doubts or if you need more inputs.

Keep the questions coming.

 
swapnil kachave
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
student s1 = new student();
In above the s1 is reference variable which point to the Object of class student .
but you can use object in function in this variable because of that there is no such requirement of creating object reference .
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote:Yup now i understand that it creates an instance of nullpointerexception class but the instance does not have a name ,
It just says throw new nullpointerexception() , It just creates a instance but with what name ?
For eg - we write student s1 = new student();
So the name is s1 for the instance ,

Objects (instances of classes) do not have names. If you do this:

then you are declaring a variable named s1, that refers to an instance of class Student. But the name of the variable doesn't have anything to do with the object that the variable refers to. So, s1 is not "the name for the instance".
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper Young explained, objects do not have names. An object exists somewhere
on the heap and the JVM holds a reference to this location. But this reference does not
have a name. It's called an anonymous reference. In several examples above, the JVM
creates a new object and then uses it immediately.

In other examples, the value of the object reference is assigned to a reference variable.
This variable does have a name. All reference variables are physically identical. In source
code they are given a name, and they also have a type.

Jim ... ...
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou avishkar
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!