Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt in Exception Catch

 
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
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
 
Marshal
Posts: 69894
278
  • 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
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably better to use Syustem.err.println() for exceptions, otherwise that is correct.
 
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
Marshal
Posts: 69894
278
  • 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.
 
Ranch Hand
Posts: 686
  • 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
Marshal
Posts: 69894
278
  • 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: 686
  • 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
Marshal
Posts: 69894
278
  • 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
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic