• 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

exception as a argument

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers
i am quite suprised at the follwing code



when new Exception() is encountered which is a throw point a normal execution of program should be disrupted.
But the above code works fine , loop executed 10 times as in normal case.

I could understand that it is a just a exception is an object which can be passed to a method just like any other object.
But still at run time exception should have been encountered on line 3.






 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's created a new Exception at line 3. But it hasn't been thrown. There's no throw new Exception... statement. And unless an Exception is thrown it's just like any other object.

It would be very unusual to use it like this. But a common occurrence would be to catch an exception and then pass it into a method (e.g. a logging method) - that's not really all that different to what's happening here.

Note that since it's an Exception, not a RuntimeException, if it was being thrown the compiler would force you to handle or declare it.
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:
It's created a new Exception at line 3. But it hasn't been thrown.


yes , i misunderstood new Exception().


i have one more doubt that Are both checked and unchecked are checked by compiler at compile time ?


 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are both checked and unchecked are checked by compiler at compile time ?



Only checked exceptions are checked by the compiler during compile time . The unchecked exceptions are not checked by the compiler . So , this is also a classification of exceptions : 1>checked
2>unchecked exceptions
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public class Thrower {
public static void main(String[] args) throws Exception {
sneakyThrow(new Exception("This is a checked exception"));
throw new Exception();
}

public static void sneakyThrow(Throwable t) {

for(int i=0; i<10; i++)
System.out.println("get"+t);
}
}



If you change this code like the above, the main method does throw the checked exception at run time. You can declare a Runtime exception too.
In order to have the method throw an exception, put "throw new XXException()" in a line, and declare it in the method. This will make the method throw the exception.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic