3
votes
4answers
376 views

Java - Is it possible/good idea to reduce chance of crashing by catching Error?

I have a class the implements A which will run a certain method of class B. There is a requirement that this A should never crash when running this operation (which is not possible, right?). To ...
3
votes
2answers
243 views

How does the JVM handle an exception thrown by the main method?

I understand exceptions, throwing them, handling them, and propagating them to a method lower in the call stack (i.e. throws). What I don't understand is this: public static void main(String[] args) ...
1
vote
3answers
127 views

Null checking whilst navigating object hierarchies

I had to implement some code which traversed a small object hierarchy to fetch a value and display it in a TextView object (this is Android / Java). I had to do this 6 times to populate 6 TextViews ...
1
vote
1answer
110 views

Why is Throwable initCause designed to be called only once?

I find it really odd that the initCause method of Java's Throwable class can only be called once, or even not at all (if the constructor accepting a Throwable was used). This makes exception chaining ...
3
votes
1answer
80 views

When to declare @throws with a Scala annotation - abstract parent or concrete implementation?

In this StackOverflow question I asked about the behavior of the @throws annotation in Scala. Now that I understand how it works I want to ask about best practices with using it. Here's a repost of my ...
1
vote
1answer
117 views

Pros and cons of custom exceptions [duplicate]

This may be an pointless question based on opinion, style, and context but I've been racking my brain trying to decide whether or not to make use of custom exceptions and would love to hear some pros ...
0
votes
2answers
202 views

IllegalStateException vs. IllegalArgumentException

In have written a function which expects the caller to pass in a configuration file like XML. Then I parse this given file and extract something which i then return. In this function (at least) two ...
0
votes
0answers
82 views

How to void checked exceptions in Java? [duplicate]

I consider checked exception for a design mistake in the Java language. They lead to leaky abstractions and a lot of clutter in the code. It seems that they force the programmer to handle exceptions ...
2
votes
2answers
113 views

Retried Operation with generic Exception

I am looking for a way to get the logic of retrying an operation in a single method while keeping the exception types of the operation. I.e., the implementation to retry an operation could look like ...
2
votes
1answer
198 views

Should we encapsulate everything in a try{} block in a Try object?

Why can't I make a class for a Try including what I try and then run that in the try {} block? Why is it impractical? class DBConnectTry extends Try { TryResponse response[] attempt(TryObject ...
3
votes
5answers
692 views

How should I handle exception that *should* never be thrown? [duplicate]

What is the best way to handle errors that shouldn't ever happen? My current way to do this is to throw an exception if the 'thing that shouldn't happen' does happen, like so: /* * Restoring from a ...
3
votes
1answer
162 views

Should I assert in packages? [duplicate]

I have read a few asserts vs exception questions already; I ask specifically about package visible classes though. When you write a package, the classes marked with "public" are exposed to the ...
18
votes
2answers
2k views

Workaround for Java checked exceptions

I appreciate a lot the new Java 8 features about lambdas and default methods interfaces. Yet, I still get bored with checked exceptions. For instance, if I just want to list all the visible fields of ...
0
votes
2answers
376 views

Is storing stack traces in database recommended?

The current system I am working on goes through a set of documents & performs some logic using the metadata of the document. If the metadata of a document is fishy..it throws an exception. The ...
5
votes
4answers
693 views

What's the reason exceptions are heavily used in managed (C# and Java) languages but not in C++? [closed]

AFAIK, a lot of C++ projects don't allow exceptions and deny them in coding guidelines. I have a lot of reasons, for example, exception is hard to handle correctly if your binary needs to be compiled ...
2
votes
3answers
482 views

Layering Design Pattern in Java clean code style

As a Java developer, I am developing trying to use the clean code rules. But in my team we are facing a concrete problem: We have a business layer offering a service called "createObject", this ...
1
vote
4answers
481 views

What's the difference between the code inside a finally clause and the code located after catch clause?

My java code is just like below: public void check()throws MissingParamException{ ...... } public static void main(){ PrintWriter out = response.getWriter(); try { check(); } ...
2
votes
1answer
329 views

Why would one transform a checked exception to an unchecked exception? [duplicate]

My buddy told me today that some programmers transform a checked exception (e.g. EJBException, SQLException...) to an unchecked (RuntimeException?) My buddy explained a call stack where if you change ...
1
vote
2answers
268 views

Java exception redundancy

To what extent should one make exceptions redundant or atomic to a method. For instance, suppose I have a method public void authenticate(String username, String password) that calls private void ...
1
vote
3answers
207 views

When is a catch clause empty [duplicate]

I saw lots of questions and explanations regarding empty catch clauses but I was never actually sure what "empty" means. Is a catch clause like this empty? It clearly isn't empty for the eye because ...
1
vote
2answers
272 views

Wrapping specific checked exception in domain unchecked ones? [duplicate]

Uncle Bob says in Clean Code book that Unchecked Exceptions should be used. Now JDK has some checked exceptions: IOException, IllegalAccessException etc. which cannot be avoided. In my application ...
2
votes
7answers
672 views

better way to define an exception thrown by a method in Java?

I know how to define exceptions and all, but I'm not sure if the way I'm doing it is the most intuitive and readable by another users. I have a very simple method I've written that I want to throw an ...
6
votes
2answers
308 views

The suffix Exception on exceptions in java

Specifying a suffix of Exception on exception classes feels like a code smell to me (Redundant information - the rest of the name implies an error state and it inherits from Exception). However, it ...
3
votes
2answers
707 views

How to remove duplicate exception block code

I have good number of Service and DAO classes which has the same set of 30 line exception code block which gets repeated and it shows up in Code Duplication report. The approach which i can think of ...
8
votes
5answers
7k views

throwing runtime exception in Java application

I am working as a contractor designing enterprise Java application for my client in the role of a technical lead. The application will be used by end users and there will be a support team who will ...
53
votes
11answers
4k views

I've been told that Exceptions should only be used in exceptional cases. How do I know if my case is exceptional?

My specific case here is that the user can pass in a string into the application, the application parses it and assigns it to structured objects. Sometimes the user may type in something invalid. ...
6
votes
2answers
1k views

Decision for Unchecked Exceptions in Scala

As a java programmer, I have always been critical of Unchecked Exceptions. Mostly programmers use it as an en-route to coding easiness only to create trouble later. Also the programs (though untidy) ...
8
votes
5answers
930 views

“Programming error” exceptions - Is my approach sound?

I am currently trying to improve my use of exceptions, and found the important distinction between exceptions that signify programming errors (e.g. someone passed null as argument, or called a method ...
2
votes
3answers
858 views

Good practice or service for monitoring unhandled application errors for a small organization

I'm working with multiple software with varying ways of monitoring for errors. When I make software, I usually send email with the stack trace to admins(usually me). Some customer software is ...
1
vote
2answers
1k views

Exception hierarchy design

In my company we are building a webapp containing serveral central services that we design ourselves and then specify as interfaces. I.e. the interfaces are application specific and they are then ...
26
votes
5answers
2k views

Is use of finally clause for doing work after return bad style/dangerous?

As part of writing an Iterator, I found myself writing the following piece of code (stripping error handling) public T next() { try { return next; } finally { next = ...
15
votes
6answers
4k views

Efficient try / catch block usage?

Should catch blocks be used for writing logic i.e. handle flow control etc? Or just for throwing exceptions? Does it effect efficiency or maintainability of code? What are the side effects (if there ...
33
votes
4answers
12k views

Is it better to use assert or IllegalArgumentException for required method parameters?

In Java, which is more highly recommended, and why? Both types will throw exceptions, so in that regard handling them is the same. assert is slightly shorter, but I'm not sure how much that matters. ...
42
votes
7answers
27k views

Why use try … finally without a catch clause?

The classical way to program is with try / catch but when is it appropriate to use try without catch? In Python the following appears legal and can make sense: try: #do work finally: #do ...
4
votes
2answers
304 views

Nulls in every type and checked exceptions in Java?

I know that null being added to every type in Java is a source of much frustration regarding the language's type system. At the same time I generally hear complaining about checked exceptions - that ...
3
votes
3answers
2k views

How to store exception messages

How are exception messages commonly stored? for any domain. I'm thinking about this from a maintenance standpoint. if(!Condition1) throw new Exception("Some exception"); if(!Condition2) ...
2
votes
3answers
716 views

What do you think of this Exception handling practice

I'm working on a project that includes a lot of creating/manipulating and reading JSONObjects and arrays but not in a systematic way. So there is JSON code everywhere. It is ok for me except that ...
8
votes
9answers
801 views

Should security restrictions cause a service to return null or throw an exception?

I'm in a bit of a disagreement with a more experienced developer on this issue, and wondering what others think about it; our environment is Java, EJB 3, services, etc. The code I wrote calls a ...
23
votes
9answers
9k views

Is it good practice to catch a checked exception and throw a RuntimeException?

I read some code of a colleague and found that he often catches various exceptions and then always throws a 'RuntimeException' instead. I always thought this is very bad practice. Am I wrong?
6
votes
5answers
5k views

Is it not a good practice to handle runtime exceptions in the code?

I am working on a Java application, and I see that Run time exceptions are handled in many places. For example, try { //do something }catch(NullPointerException e){ ...
20
votes
8answers
4k views

What to put in a try/catch? [duplicate]

Note on the question: this is not a duplicate, Efficient try / catch block usage? was asked after this one. The other question is the duplicate. I was wondering what was the best way to use ...
4
votes
3answers
473 views

Improving exception handling? [duplicate]

I am a newbie programmer and I recently started learning about exception handling in Java. I know what try, catch and finally blocks do, but I really need to understand how to use them well and where ...
11
votes
1answer
4k views

Why does Java exit with success after an uncaught exception?

Any time a Perl, Python, C++ or Tcl program halts with an unhandled exception, those language runtimes take care to register a non-zero exit code for the process. Even Eclipse-based programs return 1 ...
12
votes
8answers
2k views

In Java, what are checked exceptions good for? [closed]

Java's checked exceptions have gotten some bad press over the years. A telling sign is that it's literally the only language in the world that has them (not even other JVM languages like Groovy and ...
19
votes
12answers
2k views

How to teach Exception Handling for New Programmers?

How do you go about teaching Exception Handling to Programmers. All other things are taught easily - Data Structures, ASP.NET, WinForms, WPF, WCF - you name it, everything can be taught easily. With ...
4
votes
2answers
411 views

How much logging should I do at production for SQLExcpetions?

I'm writing a small database-centric application in Java Swing that will be used internally in a small company. I haven't much experience with developing production-level applications. I don't use an ...