Hello, I'm new in Java programming, just learning it and I want to ask you about the Exceptions. As much as I understood, I just need to create a class e.g. "MyException" that extends the Exception class, and use the exception specification "throws" in the function that might throw the particular exception, and after that throw the exception inside the function. I also read about " try" and "catch ", but what I can't get clear in my mind is the following: Do I need to write in the class "MyException" that e.g. if something doesn't happen, only then throw the exception... or the compiler will know when to throw the exception... I really don't get it. If I don't want the user to be able to insert integers in the file, just String, do I specify this somewhere, or just write "try" and "catch(Exception e)" ?
Jump to Post
An important thing to understand is that there are two types of exceptions:
- checked exceptions, which are thrown when your code (or some code in the Java libraries) explicitly throws them; in this case, methods that throw such exceptions must be declared as throwing them, and code that calls …
Jump to Post
OK, so in this specific example, absolutely any sequence of bytes can be a valid string. So when you read a string from the file-- I assume you're thinking of one "string" per line, so you'd generally use BufferedReader.readLine()-- Java can't predict that what you actually want is strings that …
All 5 RepliesReply to this topic
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.