A question about sorts of using exception

1,395
0

Main Question or Discussion Point

i know only the try catch method
that says
for that king of excepion do this

but i saw some weird other implementation
like in the signature of the method "throws exception"
ok now what? it doesnt say what to do in a case of an exception
it just say "throws exception"
what other sorts of exception implementation are there??
 

Answers and Replies

237
0
I'm having a hard time understanding your question.

You don't need (in most languages anyway) to catch a specific type of exception.

Code:
try { int a = 1 / 0; }
catch (DivisionByZeroException e) { Console.Out.WriteLine("Division by Zero"); }
vs

Code:
try { int a = 1 / 0; }
catch (Exception e) { Console.Out.WriteLine("Something bad happened"); }
(This is c#, your exact vocabulary may vary).

If you catch an unspecified exception, it is sort of up to you what you want to do about it. It depends on the situation. Sometimes you might want to just fail silently (if say your program is checking for updates of itself and cant connect to the server), sometimes you want to display the details of the exception to the user (for most errors) and leave "what to do next" to him or her, and sometimes you want to shut down completely.

k
 
1,395
0
i forgot to mention that this is java
 
1,395
0
can you answer this question for JAVA
 
237
0
In java, all exceptions inherit from the Exception class, so you can catch that one if you don't know what you are looking for.

Code:
try 
{ 
    .. 
}
catch (Exception e) 
{
    System.err.println("Error :" + e.getMessage());
}
k
 
Last edited:
1,395
0
ye but thats not the only sort
there is the case of a method throws exception

were is the catch stuff there
cause i saw implementation without any catch in this sort of implementation
 
CRGreathouse
Science Advisor
Homework Helper
2,817
0
Are you saying that you throw classes that don't derive from the Exception class? In that case you'd have to know what to catch since you wrote the code. But in general you should only throw Exceptions; you should add "extends Exception" to any class you're throwing. (N.B.: not to any class that throws exceptions, only classes that are themselves exceptions!)
 
-Job-
Science Advisor
1,124
1
In Java, when you're calling any methods that may throw a given exception, then your options are to use a try/catch to handle the exception or to mark your current method as able-to-throw that exception (using "throws").

This ensures that, even though you're choosing not to handle a given exception in your code, the code that is calling your method will handle that exception itself (or its caller, etc). You use "throws" to delegate the handling of the exception to the caller.
 

Related Threads for: A question about sorts of using exception

  • Last Post
Replies
1
Views
1K
Replies
6
Views
770
Replies
4
Views
4K
Replies
6
Views
2K
  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
3
Views
480
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
2
Views
495
Top