Questions tagged [try-catch]

try-catch is a syntactic construct for catching exceptions raised by a code section

The try-catch construct for exception handling is used by a wide group of languages, like C#, C++, Matlab, Python, Java, and JavaScript. An example of this construct is shown below.

try
{
    Some code
}
catch(exception e)
{
    Handle exception
}

References:

8733 questions
3
votes
5 answers

What to include in the catch clause of Exception

I have a code that throws a bunch of Exceptions but each of them only contains a printStackTrace() method as shown below } catch (SecurityException e) { // TODO Auto-generated catch block System.err.println(e); e.printStackTrace(); }…
Epitaph
  • 3,128
  • 10
  • 34
  • 43
3
votes
4 answers

Do while goes infinite with try catch

I have a problem when trying to execute try-catch statement inside do while loop.I ask user to first enter letter and then a number and if he enters number correctly the program ends.If he enters letter instead of number the program should say "An…
Stanimir
  • 51
  • 6
3
votes
1 answer

How to use a PreparedStatement twice with Try-with-Resources?

When using PreparedStatements in a regular Java Try-Catch block, I can change the PreparedStatement to run different queries whenever I need, like so: String sqlStatement = "update someTable set someValue = true"; try{ PreparedStatement pstmt =…
jabe
  • 784
  • 2
  • 15
  • 33
3
votes
2 answers

Rethrowing an exception in C#

I have some code which catches the exception, rolls back the transaction and then rethrow the exception. catch ( Exception exSys ) { bqBusinessQuery.RollBackTransaction(); throw exSys ; } If I use this code, VS Code analysis throws…
jitendragarg
  • 945
  • 1
  • 14
  • 54
3
votes
4 answers

How to check if Thread code runs in try-catch block

I would like to validate our code and check if every Thread that we execute runs in try catch block. A valid sample: Thread loadDataThread = new Thread(new ThreadStart(LoadData)); public void LoadData() { try {/*do something*/} catch(Exception…
Toni Frankola
  • 1,652
  • 15
  • 26
3
votes
2 answers

Check for the existence of a record in Access database using C#

First, I did search for this first and found this question answered: Previous Answer The problem I have with this answer is that using this method causes a NullReferenceException. Obviously the code will still work with a try/catch block, but I had…
Keven M
  • 972
  • 17
  • 47
3
votes
1 answer

Throw exception in Java

Suppose I have a class, the requirement is that "change the following code to throw checked exception if "den" is 0, and change the function to catch that exception". public class divide { void divide(int num, int den){ …
user6119494
  • 65
  • 1
  • 7
3
votes
3 answers

try-catch in method having return object T

I have a method in c#, in which it return an object! I need to use a try catch here this is my method public T FromBinary() { T obj; try { using (Stream stream = File.Open(this.serializeFilePath, FileMode.Open)) { …
S M
  • 3,133
  • 5
  • 30
  • 59
3
votes
1 answer

How to convert NSKeyedArchiver objects over new versions

I just released an app to the app store and one of my patrons let me know that I should change the data type that I was previously storing as an Integer, using the NSKeyedArchiver, to a Double. Easy enough to change the app's data model but when I…
3
votes
4 answers

Use try/catch in manner of if else, but with exceptions as condition?

If I search in a list for an element and don't find it, I throw a NotInListException otherwise I want to add it to another list I achieved this by try { element = actualList.find("foo"); anotherList.append(element); } catch…
jam
  • 1,253
  • 1
  • 12
  • 26
3
votes
1 answer

Angular JS : $exceptionHandler without try or throw block?

app.factory('$exceptionHandler', function() { return function(exception, cause) { exception.message += ' (caused by "' + cause + '")'; throw exception; }; }); Is it possible to handle all the exception globally in…
shreyansh
  • 1,637
  • 4
  • 26
  • 46
3
votes
2 answers

Detecting if a final is blank in a constructor

I'm trying to create an enum for final Images, where the variable 'image' would be loaded from a file. If an IOException occurs, I want 'image' to be set to null. However, according to the compiler, 'image' may or may not be set when the catch block…
user5770298
3
votes
1 answer

PHP (or other): Strategy to deal with exceptions that "cannot occur"

Consider the following code. class C {} /** * @throws \InvalidArgumentException */ function classCreateInstance($class) { if (!is_string($class)) { throw new \InvalidArgumentException("Class name must be a string."); } if…
donquixote
  • 4,877
  • 3
  • 31
  • 54
3
votes
3 answers

Swift 2.1 do-try-catch not catching error

Here's my Swift 2.1 code snippet. The error that's occurring is shown in the comments at the point where the error appears. The error shows up in the debugging panel, and the app crashes. The app never prints the line in the catch, nor does it…
leanne
  • 7,940
  • 48
  • 77
3
votes
2 answers

Java Try Catch block

I initially started programming in college and learnt vb.net. Now I have decided to make the move to Java and have some queries. In vb, the try catch statement is laid out as follows try Catch ex as exception finally End catch but from the java…
User59
  • 487
  • 4
  • 19
1 2 3
99
100