A question about sorts of using exception

  • Thread starter transgalactic
  • Start date
In summary, you can use a try/catch to handle specific exceptions, or you can mark your methods as being able to throw specific exceptions.
  • #1
transgalactic
1,395
0
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 doesn't say what to do in a case of an exception
it just say "throws exception"
what other sorts of exception implementation are there??
 
Technology news on Phys.org
  • #2
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 can't 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
 
  • #3
i forgot to mention that this is java
 
  • #4
can you answer this question for JAVA
 
  • #5
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:
  • #6
ye but that's 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
 
  • #7
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!)
 
  • #8
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.
 

What is an exception in programming?

An exception in programming refers to an unexpected event or error that occurs during the execution of a program. It can be caused by a variety of factors, such as invalid user input, hardware malfunctions, or programming errors. When an exception occurs, the program stops running and an error message is displayed.

What are the different types of exceptions?

There are two main types of exceptions: checked exceptions and unchecked exceptions. Checked exceptions are errors that occur during compile time and require the programmer to handle them using try-catch blocks. Unchecked exceptions, on the other hand, occur during run time and do not necessarily require specific handling by the programmer.

How can exceptions be useful in programming?

Exceptions are useful in programming because they help identify and handle errors in a program. By using try-catch blocks, programmers can anticipate potential errors and implement error handling strategies to prevent the program from crashing. Exceptions also make code more readable and maintainable by separating error handling logic from regular program logic.

What is the purpose of the "finally" block in exception handling?

The "finally" block in exception handling is used to execute code that should always run, regardless of whether an exception is thrown or not. This block is typically used to perform cleanup tasks, such as closing open files or database connections, to ensure that the program runs smoothly even in the event of an exception.

How can we prevent exceptions from occurring in our programs?

While it is impossible to completely prevent exceptions from occurring, there are steps that programmers can take to minimize their occurrence. These include validating user input, using proper error handling techniques, and testing the program thoroughly before deployment. It is also important to understand and anticipate potential errors in the code to prevent them from causing exceptions.

Similar threads

  • Programming and Computer Science
Replies
2
Views
603
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
19
Views
962
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
3
Views
766
  • Programming and Computer Science
Replies
5
Views
600
Replies
8
Views
1K
Back
Top