wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception as a argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception as a argument" Watch "exception as a argument" New topic
Author

exception as a argument

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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.






Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Oct 23, 2011
Posts: 384

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 ?


ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
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
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception as a argument
 
Similar Threads
Name Of file !!!
constructor
casting problem
How many objects eligible for GC?
Question #2 from Doug's book