# Catch error on vtkDICOMWriter

**URL:** https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664
**Category:** Support
**Created:** [June 29, 2020, 2:13pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664 "2020-06-29T14:13:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [June 29, 2020, 2:13pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/1 "2020-06-29T14:13:26Z")

</div>

I have the following code:

```
vtkDICOMMRGenerator* pGenerator = vtkDICOMMRGenerator::New();
vtkDICOMWriter* pWriter = vtkDICOMWriter::New();
pWriter->SetInputConnection(m_pImageExtractor->GetOutputPort());
pWriter->SetGenerator(pGenerator);
pWriter->SetPatientMatrix(m_pDICOMReader->GetPatientMatrix());
pWriter->SetMetaData(m_pDICOMReader->GetMetaData());
pWriter->SetFilePrefix(sPathName);
pWriter->SetFilePattern(_T("%s/IM-0001-%04.4d.dcm"));
try
{
	pWriter->Write();
}
catch (std::exception& error)
{
	m_sState = error.what();
}

```

**sPathName** has value: d:\tempx\output\

but at **Write** method, I got crash:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/f/fc7eb50cae8a183227f48f5133d71c4bc8f0132a.png)

How can correctly catch the error in VTK ? Is this way, I would know the source of my error …

P.S. The example of vtkDICOMWriter I’ve taken from here: [https://github.com/dgobbi/vtk-dicom/blob/master/Examples/TestDICOMWriter.cxx](https://github.com/dgobbi/vtk-dicom/blob/master/Examples/TestDICOMWriter.cxx)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 30, 2020, 2:15am UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/2 "2020-06-30T02:15:46Z")

</div>

You only try to catch C++ exceptions and you cannot catch all exceptions anyway, so I would recommend to focus on fixing the root cause of the error.

Using \_T for string literals is decade-old technology, it never got widely adopted and I don’t think anybody tested it with VTK in recent years. Instead, I would recommend to use UTF-8 everywhere. In current Windows versions, you can even set the application’s code page to UTF-8.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 30, 2020, 10:52am UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/3 "2020-06-30T10:52:54Z")

</div>

Hello,

> [@flaviu2](#):
>
> **sPathName** has value: d:\tempx\output\

The sequence `\t` is an escape sequence for the tab character in C-like languages. Try using `\\t` to have a \t in your path string instead of a tab.

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 30, 2020, 10:57am UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/4 "2020-06-30T10:57:25Z")

</div>

You can also use `/` in place of `\` as the recent versions of Windows regonize paths with forward slashes, then you can avoid that kind of bug.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [June 30, 2020, 1:15pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/5 "2020-06-30T13:15:15Z")

</div>

I guess is not this problem, the code looks like that:

**\_T(“D:\\Tempx\\Output”)**

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 30, 2020, 1:23pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/6 "2020-06-30T13:23:03Z")

</div>

Make sure you have full access rights to the path.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [June 30, 2020, 3:17pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/7 "2020-06-30T15:17:31Z")

</div>

I have the following code:

```
vtkDICOMMRGenerator* pGenerator = vtkDICOMMRGenerator::New();
vtkDICOMWriter* pWriter = vtkDICOMWriter::New();
pWriter->SetInputConnection(m_pImageExtractor->GetOutputPort());
pWriter->SetGenerator(pGenerator);
pWriter->SetPatientMatrix(m_pDICOMReader->GetPatientMatrix());
pWriter->SetMetaData(m_pDICOMReader->GetMetaData());
pWriter->SetFilePrefix(_T("E:/Output/"));
pWriter->SetFilePattern(_T("%s/IM-0001-%04.4d.dcm"));
TRACE(">>>>>>%s|%s|%s\n", pWriter->GetFileName(), pWriter->GetFilePattern(), pWriter->GetFilePrefix());
pWriter->Write();
pWriter->Delete();
pGenerator->Delete();

```

With the following outcome:

```
>>>>>>(null)|%s/IM-0001-%04.4d.dcm|E:/Output/

```

But the strange thing is coming now: in **Debug** is crashing as I wrote in first post, in **Release** is run without problem … why ?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 30, 2020, 3:52pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/8 "2020-06-30T15:52:43Z")

</div>

If it is crashing in debug mode then it’s awesome, you can then see exactly what goes wrong. Just run the code in a debugger and have a look at the call stack, inspect variables, etc.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [June 30, 2020, 4:19pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/9 "2020-06-30T16:19:39Z")

</div>

I’ll tried that, but only on my app variables, because I don’t have cxx source files from _VTK_ or _VTK-DICOM_ … I got header files and lib files for _VTK_ and _VTK-DICOM_ and I only included in my project.

Anyway, at the line

```
pWriter->Write();

```

my program stop with that crash (see first post).

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [July 1, 2020, 6:38am UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/11 "2020-07-01T06:38:40Z")

</div>

@lassoan I guess I cannot do that, I could find what is the problem inside of VTK-DICOM class [vtkDICOMWriter](https://dgobbi.github.io/vtk-dicom/doc/api/classvtk_d_i_c_o_m_writer.html) but I don’t have cxx file, I got from someone only header files and lib files already compiled.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 1, 2020, 11:49am UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/12 "2020-07-01T11:49:32Z")

</div>

Do you use Visual Studio? If so, you need to double check whether your peer used the same compiler to build the DLLs. If your peer used Visual Studio 2015 to compile VTK, then you should compile your program with it as well.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [July 1, 2020, 12:36pm UTC](https://discourse.vtk.org/t/catch-error-on-vtkdicomwriter/3664/13 "2020-07-01T12:36:54Z")

</div>

I think I found the source of the issue: for patient matrix I used vtkDICOMReader but for input connection I used vtkImageExtractComponents.
