• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Exception Catch

 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you written both catch (ClassNotFoundException) and throws ClassNotFoundException, stanislav bashkirtsev?
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is this correct, as both the exception are caught
 
Campbell Ritchie
Sheriff
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably better to use Syustem.err.println() for exceptions, otherwise that is correct.
 
Kartik Tal
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# 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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, Fred, but let's imagie that printStackTrace() is just a placeholder for something more interesting in the catch.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic