use of "throws IOException" in java file handling [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
why we use 'Throws IOException' in java File handling. i mean what is the purpose of using these two words in File handling??
Please help...

You are probably referring to checked exceptions. They are used to indicate that a method could potentially throw one of the listed exceptions so that the callers need to explicitly catch them or be marked with the same exceptions list.

The throws word means that the code could enter an error state, the IOExeption word describes the error state.
This means that you will have to put any method that throws IOExcepion into a try ... catch block where you can write code to make your program respond to the error state appropriately.

Related

Exception(checked and unchecked) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There are 2 types of exceptions. Checked and unchecked. Checked is defined as an exception that occurs at compile time and unchecked is an exception caught at runtime. Usually an exception is always caught at runtime. So how is it possible that checked exception is caught at compile time.
So how is it possible that checked exception is caught at compile time.
The compiler can check if the method or constructor that is being called has been declared to throw a checked exception which is information that is available to the compiler at compile time. With this information, the compiler can check to see if the calling code either throws or catches the exception and then give an error if this is not occurring.
Note that the exception may or may not be thrown, but only during run-time, and so it is only truly fully handled at run-time, if it occurs.

Using exception or using multiple try catch java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am just writing a code and want to know whether it is good to use exception class to catch an exception or using multiple type of try-catch.
try{
// some error-prone code
}catch(Exception e){
}
or,
try{
// some error-prone code
}catch(NullPointerException n){
}catch(ArrayOutOfBoundException a){
} ..... etc
It depends on what will you do with the exception. If you have specific things to do per exception then you can use multiple try catch. If not you can just use the generic exception. Also if you know what kind of exception your code might throw better just use that specific exception.
You should only catch exceptions that you know how to process properly; examples include IOException where you can do something like retry an operation, return some default value, or rethrow the error; and NumberFormatException, where you're trying to read user input as a number and find that it's garbage, so you can ask the user to try again.
In nearly all cases, you don't actually know what the proper response to "any error" is, and in many cases (most unchecked exceptions, for example), the only thing you'll be doing by catching Exception is masking some underlying problem that needs to be resolved. In general, the only response that is acceptable for a generic unknown exception is "write a log message and abort the current operation" for whatever definition of "current operation" applies (might include things like rolling back a transaction and returning an HTTP 503 status code).
In real-world applications, this last-resort catch Exception is handled by framework code (such as Spring or Jersey) and does these broad cleanup operations. If your code can't do anything better (which generally requires knowing what specifically happened, not just "an exception"), then it should let the exception propagate and use the standard error handlers.

Checked Exception is compile time or runtime? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I read about Checked exception that it is checked by compiler , but its runtime only.
Is it correct?
If it is correct then how?
Checked exceptions are checked at compile time to ensure you are handling them, either by catching them or declaring the containing method throws the exception.
At runtime, there is no distinction between checked and unchecked exceptions: they are treated identically by the JVM. So "checked-ness" is purely a compile-time concept.
Runtime exceptions are not checked exceptions. Checked exceptions are the class Exception and all subclasses except for RuntimeException.
Checked exceptions need to be either handled in your code or declared in the method's signature otherwise your code won't compile. Runtime exceptions do not need to be handled or declared.

Best exception to use in Java for bad xml input [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a class in java (for android) that accepts an org.w3c.dom.Element as input and provides some business logic functions around it. In one of those functions, I call getElementsByTagName and expect only 1 element in the returned NodeList. If there's more than 1 element, it means the Element I got is jacked up somehow and I need to throw an exception. Is there an exception already included in JDK7 which is made to accurately represent this problem?
You could make the method throw an IllegalArgumentException:
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
Or you could write your own Exception class which may be a better solution if you're following this exception-driven approach.
Note however that using exceptions for flow control can cause lower performance.
It's best to define your own Exception, as none of the Java defined exceptions quite fit the case you're experiencing, and it doesn't seem that people outside your organization need to understand the thrown exception.

Exception questions Java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
For example if I was making an image API in Java would it be more beneficial to have multiple exception classes such as ImageSizeException, ImageFormatException or have a single exception class such as ImageException which has a string, enum ect for what kind of exception it is.
As a secondary question if you use multiple exception classes would it be more acceptable to group them in an exceptions package or with the classes that would throw them
You should certainly have a base exception, ImageException, which is the superclass of any others. Like Java has IOException.
Personally I would prefer one Exception with an Enum or int to subtype, but typical Java style is to have a lot of Exception subclasses. Your should probably follow convention.
It's Better to have many exceptions.it will be easier debugging when your code is huge,and you could have ImageException as the superclassfor all these exceptions.

Categories