The moose likes Java in General and the fly likes what happens if we dont use a throws exception to a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "what happens if we dont use a throws exception to a method?" Watch "what happens if we dont use a throws exception to a method?" New topic
Author

what happens if we dont use a throws exception to a method?

senthil sen
Ranch Hand

Joined: Oct 10, 2002
Posts: 182
what happens if we dont use a throws exception to a method?will the jre catch the exception automically?
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
it depends!!!
if u are writing a method that implicitly or explicitly throws any checked exception u must catch it, so when u try to compile this code the compiler refuse to compile, the checked exception (the ones like InterruptedException, or IOException) must be catched in a try/catch block.
for example:

public class test{
publi static void main(String args[]){
Thread.sleep(200);
}
}
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
it depends!!!
if u are writing a method that implicitly or explicitly throws any checked exception u must catch it, so when u try to compile this code the compiler refuse to compile, the checked exception (the ones like InterruptedException, or IOException) must be catched in a try/catch block.
for example:

public class test{
publi static void main(String args[]){
Thread.sleep(200);
/*this wont compile, there must be a try/catch block*/
}
}
to solve u have two options:
place the Thread.sleep(200) inside a try/catch or make the main method subject of throwing an exception:
public static void main(String[] args) throws Exception{
//code
}
if there isn't any catch to handle the exception, the current Thread will end, and there will be a message in the standard output!!!
 
 
subject: what happens if we dont use a throws exception to a method?
 
Threads others viewed
try catch doubt
Can main function use Throws ?
Which one to select between Classic custom tag and Simple custom tag
Garbage Collection
how to synchronize service() in servlets with out using singlethreadmodal interface
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture