my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Doubt in Exception Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Exception Catch" Watch "Doubt in Exception Catch" New topic
Author

Doubt in Exception Catch

Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83



in the above code in catch block the exception caught is of IO. But the method also throws ClassNotFoundException , so where will this (ie. ClassNotFoundException) exception be caught.
stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Yes, that's the way you can provide catch for multiple exceptions.

If you don't provide an catch for a possible exception it would be passed on to the next method in the call-stack. Anyway at some point it needs to be caught. An exception that's never caught will cause your application to stop running!

Regards,
John Eipe


www.cs-repository.info
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Why have you written both catch (ClassNotFoundException) and throws ClassNotFoundException, stanislav bashkirtsev?
Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83


is this correct, as both the exception are caught
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Probably better to use Syustem.err.println() for exceptions, otherwise that is correct.
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
# private void readObject(ObjectInputStream ins){
# try{
# ins.defaultReadObject();
# dept = new Department(ins.readInt(),ins.readUTF());
# }catch(IOException e){
# System.out.println(" IO EXCEPTION : "+e);
# }catch(ClassNotFoundException e){
# System.out.println(" CLASS CAST : "+e);
# }
# }


This is the way in which you are supposed to catch Exceptions, but there will be case where you dont know what kind of exception it will throw then you have to give

# catch(Exception e){
# System.out.println(" Exception: "+e);
# }

It will catch ALL exceptions. And if you include this catch at first the others wont execute.
stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
Probably better to use Syustem.err.println() for exceptions, otherwise that is correct.
I think e.printStackTrace() is more natural
It will catch ALL exceptions. And if you include this catch at first the others wont execute.
But it's not a good idea to catch all exceptions in that way.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Agree that printStackTrace is better than println(e); the point I was making is that one ought to use System.err not System.out for exceptions. And agree that catch (Exception ex) is not good design; you may have problems if you don't know what sort of Exception to catch.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Well, I'd say yeah too , except that if you are just going to print the stack trace anyway, then it hardly matters that you have just used a single catch. And if the situation is such that you don't need to know the what kind of exception, or if they are to be treated in the same way, then why bother?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I agree, Fred, but let's imagie that printStackTrace() is just a placeholder for something more interesting in the catch.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:I agree, Fred, but let's imagie that printStackTrace() is just a placeholder for something more interesting in the catch.


Yes, when you put it that way then I would go along.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Fred Hamilton wrote:Yes, when you put it that way then I would go along.
Even with the spelling error, it is nice to get agreement
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Exception Catch